Async Generator Basics
NodeJS
Hard
4 views
Problem Description
Create an async generator that yields values with delay.
Output Format
Print lines.
Constraints
Use for await...of.
Official Solution
function delay(ms) {
return new Promise((r) => setTimeout(r, ms));
}
async function* gen() {
for (let i = 1; i <= 3; i++) {
await delay(10);
yield i;
}
}
(async () => {
for await (const v of gen()) console.log(v);
})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!