stream-iterators-utils

0.1.1 • Public • Published

Stream Iterators Utils

Build Status

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

Dependencies (0)

    Dev Dependencies (4)

    Package Sidebar

    Install

    npm i stream-iterators-utils

    Weekly Downloads

    25

    Version

    0.1.1

    License

    MIT

    Unpacked Size

    5.83 kB

    Total Files

    6

    Last publish

    Collaborators

    • matteo.collina