codequick-darkmode-logo
ConnexionS'inscrire
  • javascript-advance

Understanding Generators in ES6

Generators are a powerful feature introduced in ES6 (ECMAScript 2015) that allow you to create iterable and asynchronous functions in JavaScript. They provide a new way to define iterators, which are objects used for looping over values. In this article, we will explore how to use generators and see some practical examples of their usage.

What are Generators?

A generator in JavaScript is defined using a special function syntax, denoted by an asterisk (*) placed after the function keyword. When called, a generator function returns an iterator object that can be used to iterate over values. Unlike regular functions, generators can pause and resume their execution, allowing for more flexible control flow.

The main benefit of generators is the ability to lazily generate a sequence of values on demand. Instead of generating all values at once, they generate them one at a time, which can save memory and improve performance in certain scenarios.

Generator Function Syntax

To define a generator function, we use the following syntax:

function* myGenerator() { // Generator function body yield 'Hello'; yield 'World'; }

In the above example, we define a generator function called myGenerator using the function* syntax. Inside the function body, we use the yield keyword to return values from the generator. Each yield statement suspends the generator and returns a value to the caller.

Using Generators

To use a generator, we need to call it and obtain an iterator object. We can do this by invoking the generator function as a regular function, followed by calling the next() method on the returned iterator:

const iterator = myGenerator(); // Calling the generator function console.log(iterator.next().value); // Output: 'Hello' console.log(iterator.next().value); // Output: 'World'

When calling iterator.next(), the generator function starts executing until it reaches a yield statement. The next() method returns an object with two properties:

  • value: The value yielded by the generator.
  • done: A boolean indicating if the generator has finished executing (true) or not (false).

Iterating with for...of Loop

Generators can be used in conjunction with the for...of loop to easily iterate over the generated values. The loop automatically calls the generator's next() method and assigns the yielded value to a variable:

function* myGenerator() { yield 'Hello'; yield 'World'; } for (const value of myGenerator()) { console.log(value); }

The above code will output:

Hello World

Generator Control Flow

Generators can also be used to implement more advanced control flow patterns. By using the yield statement, we can pause execution and later resume it from the same point. This allows us to build asynchronous-like behavior without callbacks or promises.

Here's an example that demonstrates how to build a simple countdown generator:

function* countdown() { let count = 3; while (count > 0) { yield count; count--; } } const iterator = countdown(); console.log(iterator.next().value); // Output: 3 console.log(iterator.next().value); // Output: 2 console.log(iterator.next().value); // Output: 1 console.log(iterator.next().done); // Output: true

In this example, we define a generator function called countdown. It uses a while loop to decrement a counter and yield the current value on each iteration. When the count reaches zero, the generator finishes execution and the done property becomes true.

Conclusion

Generators are a powerful addition to the JavaScript language, allowing for both synchronous and asynchronous control flow. They provide a concise and expressive way to define iterators for various use cases.

To learn more about generators and explore their capabilities, consult the following resources: