Draft
Conversation
FeedParser instances can now be consumed directly with for await...of. The implementation bridges the push-based stream event model to the pull-based async iteration protocol using an async generator, without changing the underlying readable-stream v2 implementation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Symbol.asyncIteratortoFeedParser.prototypeso instances can be consumed directly withfor await...ofasync function*,for await...of)test/examples.jsHow it works
A Node.js Readable stream and an async iterator are two different ways of consuming sequential data. The stream world is push-based and event-driven — data arrives and fires events. The async iterator world is pull-based — a consumer asks "give me the next item" and waits.
readable-streamv2 only knows about events. To usefor await...of, we need to bridge between them.Why
for await...ofneedsSymbol.asyncIteratorWhen JavaScript sees:
It looks for
feedparser[Symbol.asyncIterator](). That method must return an object with anext()method that returns a Promise resolving to{ value, done }. An async generator function (async function*) automatically produces exactly that object — the language handles the plumbing.The bridge: how the async generator works
Step 1 — Drain what's already buffered:
The stream buffers items internally.
read()returns them synchronously if they're already there.yieldhands each one back to thefor awaitconsumer.Step 2 — Wait when the buffer is empty:
When
read()returnsnull, there's nothing buffered yet. We create a Promise and store its resolver in the outerresolvevariable — thenawaitit. The generator is now suspended, releasing the event loop.Step 3 — Events wake it up:
When the stream has more data, it fires
readable. Our handler calls the stored resolver, which fulfills the Promise — which wakes the suspended generator. It loops back toread()and drains again. The same pattern handlesend(setsended = true, wakes up → generator breaks the loop) anderror(stores the error, wakes up → generator throws it).Step 4 — Cleanup:
If the consumer breaks early (
breakinsidefor await) the generator is garbage-collected, but first thefinallyblock runs. Without this, the event listeners would leak — the stream would hold references to handlers that call a stale resolver.Why not upgrade
readable-stream?readable-streamv3+ implements this same bridge internally on every Readable. But upgrading would change the stream implementation for everyone using this library — that's a behavioral change we don't control. Implementing it directly onFeedParser.prototypeadds the bridge on top of the existing, stable stream behavior, touching nothing underneath.Usage
🤖 Generated with Claude Code