Invert data flow: push writes on one side, iterate on the other.
WritableIterable bridges the gap between push-based (write) and pull-based (iterate) programming models. It's useful when you need to feed data into an AsyncIterable from imperative code.
Important: You must call close() when done writing, or iteration will hang.
Why use this?
- Convert callback-based APIs to AsyncIterable
- Feed data to process stdin programmatically
- Bridge between different async patterns
- Proper error propagation
Basic usage
Basic usage
import { WritableIterable } from "jsr:@j50n/proc"; const writable = new WritableIterable<number>(); // Write in background (async () => { await writable.write(1); await writable.write(2); await writable.write(3); await writable.close(); })(); // Read for await (const item of writable) { console.log(item); }
Error propagation
Error propagation
import { WritableIterable } from "jsr:@j50n/proc"; const writable = new WritableIterable<number>(); (async () => { await writable.write(1); await writable.close(new Error("something failed")); })(); try { for await (const item of writable) { // Process items } } catch (error) { console.error("Error:", error); }
[Symbol.asyncIterator](): AsyncIterator<T>
It is an AsyncIterable<T>.
private
addEmptyPromiseToQueue(): void
Add an unresolved promise to the end of the queue.
close(error?: Error): Promise<void>