This is a toolbelt of functions to facilitate the usage of async iterators with Streams in Node.js. Requires Node 10+.
npm i stream-iterators-utils
Create a new Readable
stream from the async generator. opts
are
passed to the Readable
constructor
const { toReadable } = require('stream-iterators-utils')
async function * generate () {
yield 'a'
yield 'b'
yield 'c'
}
const stream = toReadable(generate(), { objectMode: true })
stream.on('data', (chunk) => {
console.log(chunk)
})
Creates a promise that is resolved when the EventEmitter
emits the
given event or 'error'
.
const { once } = require('stream-iterators-utils')
async function run () {
const ee = new EventEmitter()
process.nextTick(() => {
ee.emit('myevent', 42)
})
const [value] = await once(ee, 'myevent')
const err = new Error('kaboom')
process.nextTick(() => {
ee.emit('error', err)
})
try {
await once(ee, 'myevent')
} catch (err) {
console.log('error happened', err)
}
}
run()
MIT