The yield keyword in JavaScript is primarily associated with generators, which are a special type of function that can be paused and resumed. Generators are useful for working with sequences of data, asynchronous programming, and creating iterators. Here are some real-life use cases and examples:
Generators allow for lazy evaluation, meaning they produce values on-demand rather than generating them all at once. This is particularly useful when working with infinite sequences.
function* infiniteSequence() {
let i = 0;
while (true) {
yield i++;
}
}
const sequence = infiniteSequence();
console.log(sequence.next().value); // 0
console.log(sequence.next().value); // 1
Generators can be used with asynchronous code to simplify complex asynchronous flows. This is often seen in combination with promises.
function fetchData(url) {
return fetch(url)
.then(response => response.json())
.then(data => data);
}
function* fetchDataGenerator() {
const data1 = yield fetchData('https://api.example.com/data1');
const data2 = yield fetchData('https://api.example.com/data2');
console.log(data1, data2);
}
const generator = fetchDataGenerator();
const { value: result1 } = generator.next();
result1.then(data => {
const { value: result2 } = generator.next(data);
result2.then(data => generator.next(data));
});
Generators can be used to create custom iterators, allowing you to define how a loop should iterate over a custom data structure.
const myObject = {
*iterator() {
yield 'a';
yield 'b';
yield 'c';
}
};
for (const value of myObject.iterator()) {
console.log(value);
}
Generators can be used to implement recursive algorithms with a more iterative style, avoiding maximum call stack size errors.
function* recursiveGenerator(n) {
if (n > 0) {
yield* recursiveGenerator(n - 1);
yield n;
}
}
for (const value of recursiveGenerator(5)) {
console.log(value);
}
The yield keyword, in these examples, is essential for pausing the execution of the generator function and passing values in and out of the generator, enabling more readable and manageable asynchronous and iterative code.