Skip to content

Instantly share code, notes, and snippets.

@yatki
Created March 22, 2019 14:18
Show Gist options
  • Select an option

  • Save yatki/f8ad12cc616ef1c4236ed830744c6c3a to your computer and use it in GitHub Desktop.

Select an option

Save yatki/f8ad12cc616ef1c4236ed830744c6c3a to your computer and use it in GitHub Desktop.
Promisify emitter.once events
const promisifyOnce = (emitter, event, reject = false) => new Promise((resolve, reject) => {
emitter.once(event, (...data) => {
if (reject) {
return reject(...data);
}
return resolve(...data);
});
});
// Usage:
const connectPromise = promisifyOnce(emitter, 'connect');
const connectErrPromise = promisifyOnce(emitter, 'error', true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment