What is the output of the following? “`js async function* generate() { for (const n of [1, 2, 3]) { yield await Promise.resolve(n * 10); } } (async () => { const results = []; for await (const val of generate()) { results.push(val); } console.log(results); })(); “`

JavaScript Professional Medium

JavaScript Professional — Medium

What is the output of the following? “`js async function* generate() { for (const n of [1, 2, 3]) { yield await Promise.resolve(n * 10); } } (async () => { const results = []; for await (const val of generate()) { results.push(val); } console.log(results); })(); “`

Key points

  • The generator function yields the results of Promises resolved with n * 10
  • The `for await` loop collects the yielded values in the results array
  • The final output is an array with values [10, 20, 30]

Ready to go further?

Related questions