
17/11/2024
JavaScript generators are a special kind of function that allow you to generate a sequence of values on-the-fly instead of computing and returning all of them at once. They are denoted by an asterisk (*) following the function keyword and utilize the yield keyword to produce values.
To create a generator, you define a function using the function* syntax. Here’s a simple example:
function* simpleGenerator() { yield 'Hello'; yield 'World'; }
In the above code, we have defined a generator function called simpleGenerator. Within this function, there are two yield statements that will produce the strings "Hello" and "World" when called.
When you call a generator function, it doesn’t execute immediately. Instead, it returns a generator object that can be iterated over. To run the generator, you need to call the next() method on this object:
const gen = simpleGenerator(); console.log(gen.next()); // { value: 'Hello', done: false } console.log(gen.next()); // { value: 'World', done: false } console.log(gen.next()); // { value: undefined, done: true }
Upon the first call to gen.next(), execution starts running until it hits the first yield, producing "Hello". The next call continues executing until it hits the next yield, which outputs "World". The final call to next() returns an object indicating what happened after the generator has yielded all its values (done: true indicates there are no more values to yield).
Lazy Execution: Generators allow for lazy evaluation. This means that values are computed and delivered one by one, only when they are needed. This can be useful in scenarios where generating all values at once would be inefficient, like processing large datasets.
Simplified Code: Generators can make complex state management simpler. Instead of nesting callbacks or handling massive asynchronous code, you can use yield to pause execution, which can lead to clean and readable code.
Infinite Sequences: Since generators do not compute all values upfront, you can create infinite sequences. Here’s an example that yields an infinite sequence of numbers:
function* infiniteNumbers() { let num = 1; while (true) { yield num++; } } const numbers = infiniteNumbers(); console.log(numbers.next().value); // 1 console.log(numbers.next().value); // 2 console.log(numbers.next().value); // 3
With the infiniteNumbers generator, you can keep obtaining new numbers endlessly by just calling next().
JavaScript generators are a treasure trove of functionality for developers. They can assist in asynchronous programming to some extent and offer a more manageable approach to iteration and value generation. With generators, you can easily pause and resume function executions while maintaining state between yields, granting powerful control over your code flow. The combination of these traits makes generators a vital tool in the JavaScript programmer's toolkit.
17/11/2024 | VanillaJS
17/11/2024 | VanillaJS
29/10/2024 | VanillaJS
18/11/2024 | VanillaJS
17/11/2024 | VanillaJS
17/11/2024 | VanillaJS
17/11/2024 | VanillaJS