Predict the output of the following javascript code
function* generatorQuiz() {
yield 1;
}
const generator = generatorQuiz();
setTimeout(() => console.log(generator.next().value), 0);
for (const value of generator) {
console.log(value);
}
Quiz Explanation
The setTimeout()
callback logs 1
immediately. However, the for...of
loop doesn't execute because generators yield values lazily. So, the overall output will be 1
and undefined
.