README
Iterator <-> stream utilities
createReadStreamFromIterator
createReadStreamFromIterator(...'abc').pipe(process.stdout);
Options:
- type:
sync
: asynchronous elements (promises, streams) are executed after the previous one endedasync
(default): asynchronous elements are executed immediately, but the order in which they were given is keptdisordered
: asynchronous elements are executed immediately and written to the stream just after they end
let delay = function (d, v) {
return new Promise((resolve) => {
setTimeout(() => resolve(v), d);
});
};
let generator = function* () {
yield delay(400, 'a');
yield 'b';
yield delay(200, 'c');
};
createReadStreamFromIterator(generator(), { type: 'sync' }); // 600ms: [a, b, c] [400, 400, 600]
createReadStreamFromIterator(generator(), { type: 'async' }); // 400ms: [a, b, c] [400, 400, 400]
createReadStreamFromIterator(generator(), { type: 'disordered' }); // 400ms: [b, c, a] [0, 200, 400]
createIteratorFromReadStream
let iterator = createIteratorFromReadStream(process.stdin);
for (let chunk of iterator) {
...
}
consumeReadStreamWithIterator
consumeReadStreamWithIterator(process.stdin, (function* () {
let chunk0 = yield 3;
let chunk1 = yield;
})());