The type of items in the iterable
The source AsyncIterable to buffer
Maximum number of items to buffer ahead (default: 10)
A new AsyncIterable with buffered access to the source
async function* slowSource() {
for (let i = 0; i < 100; i++) {
await delay(100); // Simulate slow data arrival
yield i;
}
}
// Buffer 20 items ahead for smoother processing
const buffered = toBufferedAsyncIterable(slowSource(), 20);
for await (const item of buffered) {
// Process items with reduced waiting time
console.log(item);
}
Creates a buffered AsyncIterable that pre-loads N elements from the source iterable.
This function provides lookahead buffering, which can be useful for:
The buffer is filled asynchronously and items are yielded as soon as they're available. When the buffer is full, it waits for items to be consumed before loading more.