-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AwaitEventEmitter: drop-in replacement for EventEmitter
It respects Promise pattern by callin "await" internally (for listeners that return a Promise).
- Loading branch information
Showing
2 changed files
with
28 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var debug = require('debug')('phantomas:AwaitEventEmitter:emit'); | ||
|
||
// https://github.com/Psychopoulet/asynchronous-eventemitter/blob/master/lib/main.js | ||
module.exports = class AwaitEventEmitter extends require("events") { | ||
emit (eventName, ...args) { | ||
var eventPromises = []; | ||
|
||
if (eventName !== 'scopeMessage') { | ||
debug(eventName); | ||
} | ||
|
||
this.listeners(eventName).forEach(fn => { | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction | ||
const ret = fn(...args); | ||
|
||
if (ret instanceof Promise) { | ||
eventPromises.push(ret); | ||
} | ||
|
||
// console.log(fn, fn.toString(), ret, ret instanceof Promise); | ||
}); | ||
|
||
// console.log('%s Promise.all', eventName); | ||
return Promise.all(eventPromises); | ||
} | ||
} |
This file contains 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