Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: support once with promise #20909

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,14 @@ myEE.emit('foo');
// a
```

### emitter.once(eventName, listener)
### emitter.once(eventName[, listener])
<!-- YAML
added: v0.3.0
-->

* `eventName` {string|symbol} The name of the event.
* `listener` {Function} The callback function
* Returns: {EventEmitter}
* Returns: {EventEmitter|Promise}

Adds a **one-time** `listener` function for the event named `eventName`. The
next time `eventName` is triggered, this listener is removed and then invoked.
Expand All @@ -439,6 +439,16 @@ server.once('connection', (stream) => {

Returns a reference to the `EventEmitter`, so that calls can be chained.

If a listener function isn't passed - returns a `Promise` for the event happening:

```js
(async () => {
console.log('connecting');
const stream = await server.once('connection');
console.log('connected');
})();
```

By default, event listeners are invoked in the order they are added. The
`emitter.prependOnceListener()` method can be used as an alternative to add the
event listener to the beginning of the listeners array.
Expand Down
4 changes: 2 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ function _onceWrap(target, type, listener) {

EventEmitter.prototype.once = function once(type, listener) {
if (typeof listener !== 'function') {
const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
return new Promise((resolve) =>
this.on(type, _onceWrap(this, type, resolve)));
}
this.on(type, _onceWrap(this, type, listener));
return this;
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-event-emitter-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const EventEmitter = require('events');

const e = new EventEmitter();

common.crashOnUnhandledRejection();

e.once('hello', common.mustCall());

e.emit('hello', 'a', 'b');
Expand Down Expand Up @@ -79,3 +81,14 @@ common.expectsError(() => {
EventEmitter.prototype.emit.apply(ee, args);
}
}

{
// verify the promise returning version of `once`
Copy link
Contributor

@vsemozhetbyt vsemozhetbyt May 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: // Verify the promise returning version of `once`. (uppercase and period)?

async function doTest() {
const ee = new EventEmitter();
setTimeout(() => ee.emit('hello'), 0);
await ee.once('hello');
}

doTest().then(common.mustCall());
}