Generators are a special type of functions in JavaScript that can pause their execution and resume it later, while preserving their state. They are denoted by an asterisk (*) after the function keyword. Generators are related to asynchronicity because they allow writing asynchronous code in a synchronous style, simplifying the handling of sequential asynchronous operations.
Key features:
Generators are a powerful JavaScript feature that allows creating functions capable of pausing and resuming their execution. This opens up possibilities for execution flow control and simplifying complex asynchronous operations.
Generators are created using generator functions and return an iterable object:
function* generatorFunction() {
yield 'First value';
yield 'Second value';
return 'Final value';
}
const generator = generatorFunction();Pauses execution and returns a value:
function* count() {
yield 1;
yield 2;
yield 3;
}Resumes generator execution:
const gen = count();
gen.next(); // { value: 1, done: false }
gen.next(); // { value: 2, done: false }
gen.next(); // { value: 3, done: true }Generators are especially useful for working with asynchronous operations:
function* asyncSequence() {
const user = yield fetch('/api/user');
const posts = yield fetch(`/api/posts/${user.id}`);
return posts;
}// Allows writing asynchronous code in synchronous style
function* processData() {
try {
const data = yield fetchData();
const result = yield process(data);
return result;
} catch (error) {
yield handleError(error);
}
}function* processLargeData(data) {
for (let i = 0; i < data.length; i += 100) {
const chunk = data.slice(i, i + 100);
yield processChunk(chunk);
}
}function* fibonacci() {
let a = 0, b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
}// ❌ yield outside generator
function regularFunction() {
yield 'value'; // Error!
}
// ✅ yield in generator
function* generatorFunction() {
yield 'value'; // Correct
}// ❌ Ignore result
generator.next();
// ✅ Use result
const result = generator.next();
if (!result.done) {
console.log(result.value);
}Generators are supported by all modern browsers. Transpilation (Babel) is required for older browsers.
Generators are a powerful tool for execution flow control and working with asynchronous operations, allowing for more readable and structured code.
What will be output to the console and why?
function* generator() {
console.log('Start');
yield 1;
console.log('Middle');
yield 2;
console.log('End');
return 3;
}
const gen = generator();
console.log(gen.next());
console.log(gen.next());
console.log(gen.next());Answer:
Start
{ value: 1, done: false }
Middle
{ value: 2, done: false }
End
{ value: 3, done: true }
Explanation:
gen.next(), generator execution starts until the first yield, “Start” is printed, and { value: 1, done: false } is returnedgen.next(), execution resumes from where it was paused, “Middle” is printed, and { value: 2, done: false } is returnedgen.next(), execution resumes again, “End” is printed, and { value: 3, done: true } is returned, where done: true indicates the generator has finished executionIt’s important to understand that generator execution is paused at each yield, not terminated. This allows preserving state between calls and continuing execution from the same point where it was paused.
Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪