Created
March 22, 2019 14:18
-
-
Save yatki/f8ad12cc616ef1c4236ed830744c6c3a to your computer and use it in GitHub Desktop.
Promisify emitter.once events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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