event-emitter-promisify
v1.1.0
Published
Utility to resolve EventEmitters as a promise
Downloads
12,212
Maintainers
Readme
event-emitter-promisify
Utility to resolve EventEmitters as a promise
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.