Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
[Squash] Remove prepend option
  • Loading branch information
jasnell committed Sep 5, 2017
commit 438e69510f2558fe74e2deb3939c718b5a0b7fcd
5 changes: 1 addition & 4 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,15 +586,12 @@ to indicate an unlimited number of listeners.

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

### async emitter.when(eventName[, options])
### async emitter.when(eventName)
<!-- YAML
added: REPLACEME
-->

* `eventName` {any} The name of the event.
* `options` {Object}
* `prepend` {boolean} True to prepend the handler used to resolve the
`Promise` to the handler queue.
* Returns: {Promise}

Creates and returns a `Promise` that is resolved with a one-time event handler.
Expand Down
17 changes: 3 additions & 14 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,7 @@ EventEmitter.prototype.prependOnceListener =
// An awaitable one-time event listener. Registers a once handler and
// returns an associated promise.
EventEmitter.prototype.when =
function when(type, options = { prepend: false }) {
if (options == null || typeof options !== 'object') {
// This must be lazy evaluated
const errors = lazyErrors();
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'options', 'object');
}
function when(type) {
lazyPromiseBinding();
const promise = createPromise();
let resolved = false;
Expand All @@ -407,13 +401,8 @@ EventEmitter.prototype.when =
// Do not use once because it will trigger a removeListener *before*
// the promise is resolved, which throws off the cancel logic when
// removeListener is called.
if (options.prepend) {
this.prependListener(type, listener);
this.prependListener('removeListener', cancel);
} else {
this.on(type, listener);
this.on('removeListener', cancel);
}
this.on(type, listener);
this.on('removeListener', cancel);
return promise;
};

Expand Down
17 changes: 0 additions & 17 deletions test/parallel/test-event-emitter-when.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,6 @@ const ee = new EventEmitter();
assert.strictEqual(ee.listenerCount('foo'), 0);
}

{
let a = 1;
ee.when('foo')
.then(common.mustCall(() => {
assert.strictEqual(a, 2);
}))
.catch(common.mustNotCall());

ee.when('foo', { prepend: true })
.then(common.mustCall(() => {
assert.strictEqual(a++, 1);
}))
.catch(common.mustNotCall());

ee.emit('foo');
}

{
ee.when('foo')
.then(common.mustCall(() => {
Expand Down