Skip to content

Commit

Permalink
Allow for once to return a promise
Browse files Browse the repository at this point in the history
To start some discussion regarding nodejs/node#20909 (comment)
  • Loading branch information
benjamingr authored May 23, 2018
1 parent 2efe30b commit bbd2c7d
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,18 @@ EventEmitter.prototype.on = function on(event, fn, context) {
* Add a one-time listener for a given event.
*
* @param {(String|Symbol)} event The event name.
* @param {Function} fn The listener function.
* @param {Function} [fn] The listener function.
* @param {*} [context=this] The context to invoke the listener with.
* @returns {EventEmitter} `this`.
* @returns {EventEmitter|Promise} `this` if a listener was passed for chaining,
* or a promise for the event if none was passed.
* @public
*/
EventEmitter.prototype.once = function once(event, fn, context) {
if (typeof fn !== 'function' && typeof Promise === 'function') {
return new Promise(function executor(resolve, reject) {
addListener(this, event, resolve, this, true);
}.bind(this));
}
return addListener(this, event, fn, context, true);
};

Expand Down

0 comments on commit bbd2c7d

Please sign in to comment.