Utility to resolve EventEmitters as a promise
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);
}
©2022–present Jesse Wright, MIT License.