Buffer an AsyncIterator before iteration.
Create an AsyncIterator over the values of an AsyncIterator, eagerly iterated.
$ yarn buffer-async-iterator
Create an AsyncIterator from iterable
, with the values eagerly buffered.
iterable
(AsyncIterable<Value> | Iterable<Value>) The iterable whose values will be buffered.write
Write A function that takesiterable
andbuffer
and returns a Promise.buffer
will not be iterated through until it resolves. (optional, defaultdefaultWrite
)buffer
(AsyncIterable<Value> | Iterable<Value>) An iterable representing the data being buffered. (optional, default[]
)
import bufferAsyncIterator from 'bufferAsyncIterator';
import got from 'got';
async function* getWebsites(sites) {
for (const site of sites) {
const {requestUrl} = got.head(site);
yield requestUrl;
}
}
// Immediate start running the generator.
const websiteIterator = bufferAsyncIterator(getWebsites(['google.com', 'bing.com', 'yahoo.com']));
// Other potentially time-consuming stuff here.
// Iteration begins only after the original generator has finished.
for await (const site of websiteIterator) {
console.log(site);
}
MIT © Matthew Fernando Garcia