What is the output of the following code? async function* asyncGen() { yield await Promise.resolve(1); yield await Promise.resolve(2); } (async () => { for await (const val of asyncGen()) { console.log(val); } })();

JavaScript Professional Hard

JavaScript Professional — Hard

What is the output of the following code? async function* asyncGen() { yield await Promise.resolve(1); yield await Promise.resolve(2); } (async () => { for await (const val of asyncGen()) { console.log(val); } })();

Key points

  • The async generator function yields values asynchronously using await
  • The for await loop iterates over the asynchronously yielded values
  • The values are logged to the console on separate ticks
  • The code demonstrates the use of async generator functions in JavaScript

Ready to go further?

Related questions