Creators to follow

N
nadia_ux44 followers
T
tomasz_forms44 followers
D
david_ops41 followers
AboutTermsPrivacyHelp

© 2026 teliprompt

OL
olivia_test
8 months ago•
Coding

Async iterators and generators in modern JavaScript: a working guide

Claude Opus 4.6
text output
#javascript
#async
#generators
#iterators
#explainer
Views
11.4K
Copies
1.8K
Likes
909
Comments
0
Copy rate
16.0%

Prompt

Write a focused, practitioner-level explainer on async iterators and generators in JavaScript.

Target reader: [reader_level]
Desired depth: [desired_depth]

Cover:
- What `Symbol.iterator` and `Symbol.asyncIterator` actually return
- The mental model for `function*` and `async function*`
- How `for await (...of)` desugars
- When to reach for generators vs. arrays vs. streams vs. Observables
- Backpressure in async iteration and why it is mostly free
- Three realistic examples: (1) paginated API consumer, (2) file line reader, (3) rate-limited queue worker
- Pitfalls: unhandled rejections inside a generator, forgetting to `return` to close, memory leaks when consumers break early
- How `AsyncIterator.prototype` helpers land in TC39 and what they change

Style:
- No emoji, no marketing fluff
- Tight prose, short paragraphs, concrete examples
- Use plain-text code fences where useful (no markdown fences inside prose)
- 350–500 words
- End with a one-line takeaway

Customise this prompt

Fill in 2 variables to personalise this prompt

Preview
Write a focused, practitioner-level explainer on async iterators and generators in JavaScript. Target reader: [reader_level] Desired depth: [desired_depth] Cover: - What `Symbol.iterator` and `Symbol.asyncIterator` actually return - The mental model for `function*` and `async function*` - How `for await (...of)` desugars - When to reach for generators vs. arrays vs. streams vs. Observables - Backpressure in async iteration and why it is mostly free - Three realistic examples: (1) paginated API consumer, (2) file line reader, (3) rate-limited queue worker - Pitfalls: unhandled rejections inside a generator, forgetting to `return` to close, memory leaks when consumers break early - How `AsyncIterator.prototype` helpers land in TC39 and what they change Style: - No emoji, no marketing fluff - Tight prose, short paragraphs, concrete examples - Use plain-text code fences where useful (no markdown fences inside prose) - 350–500 words - End with a one-line takeaway

Example output

Async iterators and generators are the two primitives JavaScript gives you to model a sequence of values that arrives over time. An iterable is any object whose Symbol.iterator method returns an iterator — an object with a next() method that yields {value, done}. An async iterable is the same idea, but next() returns a Promise, and the object exposes Symbol.asyncIterator instead. That is the entire contract. Every language feature you have been using — for..of, for await..of, spread, Array.from — is just sugar over those two protocols. The easiest way to produce an iterator is a generator function. Inside a function*, each yield pauses execution and hands a value to the caller; calling .next() resumes it. An async function* is the same, except yielded values are awaited on the way out and next() always returns a Promise. A for await..of loop desugars to a call to [Symbol.asyncIterator](), followed by a while loop that awaits .next() and breaks when done is true. If you throw inside the loop, the runtime calls .return() on the iterator so it gets a chance to release resources — which is why generators are a surprisingly good fit for anything that owns a handle, a connection, or a cursor. Use a generator when your source is pull-based and each step depends on the last — paginated APIs, log tailers, queue workers. Use a plain array when all values exist in memory. Use a Node stream or a Web ReadableStream when you specifically need byte-level backpressure and piping. Use Observables when you need multicast and time-based operators. Backpressure is mostly free with async iteration because the producer is paused at yield until the consumer asks for the next value. Three compact examples make this concrete: a paginated client that yields users across pages, a file reader that yields one line at a time by awaiting chunks from fs.createReadStream, and a worker that reads from a queue and yields results while respecting a semaphore so you never have more than N jobs in flight. The landmines are real. An unhandled rejection inside a generator surfaces as an unhandled promise if the consumer has already broken out of the loop. Always wrap cleanup in try/finally and trust return() to run. When a consumer breaks early, iterators that hold sockets or cursors must close them in that finally, or you will leak connections under load. Finally, watch TC39's iterator-helpers proposal — once .map, .filter, .take, and .toArray ship natively on async iterators, most of your ad-hoc utility wrappers go away. Takeaway: treat async iterators as the default shape for anything that produces values over time with bounded memory.

Related prompts

TypeScript type narrowing cheat sheet with runnable examplesby @marcus_react
Coding
CLI tool builder with argument parsing and colored outputby @nikolai_rust
Coding
Convert JavaScript to strict TypeScript with full type safetyby @marcus_react
Coding
Legacy JavaScript to modern ES2023 refactorby @marcus_react
Coding