Skip to the solution
HardFrontend6 min read

What are async iterators and for await...of?

1.0k views

The problem

Question presented to candidate: "Does for await...of only work on async generators, or can it also be used on a plain array containing Promises? What about a custom object you write yourself β€” what does it need to implement?"

What a strong answer should cover:

  • πŸ“Œ Interview term: the async iterable protocol β€” an object implementing [Symbol.asyncIterator](), returning an async iterator whose next() genuinely returns a Promise of { value, done }, rather than the plain synchronous object the regular iterator protocol (covered in this bank's own dedicated question) returns.
  • πŸ“Œ Interview term: for await...of β€” consumes an async iterable, genuinely awaiting each value in turn before running the loop body β€” verified directly with a real async generator involving a genuine mid-sequence delay.
  • πŸ“Œ Interview term: the real, direct answer to the prompt's first question β€” verified directly: for await...of genuinely works on a PLAIN array of Promises too, not just a true async generator/iterable β€” arrays are already regular (synchronous) iterables, and for await...of genuinely awaits each yielded value regardless of whether the underlying iterable is truly async or merely synchronous-but-yielding-Promises.
  • πŸ“Œ Interview term: the real, direct answer to the prompt's second question β€” verified directly: a hand-written custom object implementing [Symbol.asyncIterator](), returning an object with a genuinely async next() method, correctly worked with for await...of β€” confirming the real, minimal requirement is exactly that one method, following the async mirror of the regular iterable protocol.
  • A precise answer names that an async function* (an async generator) is genuinely both an async iterator AND an async iterable simultaneously β€” the identical dual-protocol relationship this bank's own regular generator-function question verifies for plain generators, just with the async variants.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; directly answering both halves of the prompt (works on plain Promise arrays too; needs exactly [Symbol.asyncIterator]) is the strong signal.

Code / implementation expected: Yes β€” real proof across all three cases (async generator, plain Promise array, hand-written custom object) is the clearest, most convincing demonstration.

  • async-iterators

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: for await...of works on a genuine async generator, a plain array of Promises, and a hand-written custom [Symbol.asyncIterator] object

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 128 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

How does SharedArrayBuffer let two Web Workers share memory directly, and what race conditions do you need to guard against?

Continue