jeswr / promisify-event-emitter

Utility to resolve EventEmitters as a promise
MIT License
0 stars 0 forks source link

event-emitter-promisify

Utility to resolve EventEmitters as a promise

GitHub license npm version build Dependabot semantic-release

Usage

By default promisifyEventEmitter returns a promise which resolves to undefined if the end event is called, and rejects if the error event is called.

import { promisifyEventEmitter } from 'event-emitter-promisify'

const stream = new Readable();
stream.push(null);
await promisifyEventEmitter(stream.on('data', () => {}));

The return value on end can also be customized. For instance:

export default function arrayifyStream<T = any>(stream: EventEmitter): Promise<T[]> {
  const array: T[] = [];
  return promisifyEventEmitter(stream.on('data', data => array.push(data)), array);
}

License

©2022–present Jesse Wright, MIT License.