events-autopromisify
v0.1.0
Published
Automatically promisifies all instance of EventEmitter
Readme
events-autopromisify
Automatically promisifies all instance of [EventEmitter]'s .once() methods
Only promisifies .once() methods (and not .on()), as promises resolve only once.
https://nodejs.org/api/events.html
Install
npm i events-autopromisifyUsage
Just require it once before your main code
require('events-autopromisify') // that's itAll subsequent EventEmitter instances will be automatically promisified
const {spawn} = require('child_process')
spawn(['cmd'])
.on('stdout', () => { /* callbacks for .on() */ })
.once('exit') // promises for .once()
.then(...)Watchout for 'error' events as they'll resolve by default, not reject.
const cp = spawn(['cmd']);
Promise.race([
cp.once('exit'),
cp.once('error').then(e => {throw e}),
])
.then(...).catch(...)