Package Exports
- stream-iterators-utils
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (stream-iterators-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Stream Iterators Utils
This is a toolbelt of functions to facilitate the usage of async iterators with Streams in Node.js. Requires Node 10+.
Install
npm i stream-iterators-utils
API
utils.toReadable(generator, opts)
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)
})
utils.once(emitter, event)
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()
License
MIT