-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
events: add once method to use promises with EventEmitter
This change adds a EventEmitter.once() method that wraps ee.once in a promise. Co-authored-by: David Mark Clements <david.mark.clements@gmail.com> PR-URL: #26078 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
- Loading branch information
1 parent
ef767a2
commit 064511e
Showing
3 changed files
with
164 additions
and
0 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
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
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,93 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { once, EventEmitter } = require('events'); | ||
const { strictEqual, deepStrictEqual } = require('assert'); | ||
|
||
async function onceAnEvent() { | ||
const ee = new EventEmitter(); | ||
|
||
process.nextTick(() => { | ||
ee.emit('myevent', 42); | ||
}); | ||
|
||
const [value] = await once(ee, 'myevent'); | ||
strictEqual(value, 42); | ||
strictEqual(ee.listenerCount('error'), 0); | ||
strictEqual(ee.listenerCount('myevent'), 0); | ||
} | ||
|
||
async function onceAnEventWithTwoArgs() { | ||
const ee = new EventEmitter(); | ||
|
||
process.nextTick(() => { | ||
ee.emit('myevent', 42, 24); | ||
}); | ||
|
||
const value = await once(ee, 'myevent'); | ||
deepStrictEqual(value, [42, 24]); | ||
} | ||
|
||
async function catchesErrors() { | ||
const ee = new EventEmitter(); | ||
|
||
const expected = new Error('kaboom'); | ||
let err; | ||
process.nextTick(() => { | ||
ee.emit('error', expected); | ||
}); | ||
|
||
try { | ||
await once(ee, 'myevent'); | ||
} catch (_e) { | ||
err = _e; | ||
} | ||
strictEqual(err, expected); | ||
strictEqual(ee.listenerCount('error'), 0); | ||
strictEqual(ee.listenerCount('myevent'), 0); | ||
} | ||
|
||
async function stopListeningAfterCatchingError() { | ||
const ee = new EventEmitter(); | ||
|
||
const expected = new Error('kaboom'); | ||
let err; | ||
process.nextTick(() => { | ||
ee.emit('error', expected); | ||
ee.emit('myevent', 42, 24); | ||
}); | ||
|
||
process.on('multipleResolves', common.mustNotCall()); | ||
|
||
try { | ||
await once(ee, 'myevent'); | ||
} catch (_e) { | ||
err = _e; | ||
} | ||
process.removeAllListeners('multipleResolves'); | ||
strictEqual(err, expected); | ||
strictEqual(ee.listenerCount('error'), 0); | ||
strictEqual(ee.listenerCount('myevent'), 0); | ||
} | ||
|
||
async function onceError() { | ||
const ee = new EventEmitter(); | ||
|
||
const expected = new Error('kaboom'); | ||
process.nextTick(() => { | ||
ee.emit('error', expected); | ||
}); | ||
|
||
const [err] = await once(ee, 'error'); | ||
strictEqual(err, expected); | ||
strictEqual(ee.listenerCount('error'), 0); | ||
strictEqual(ee.listenerCount('myevent'), 0); | ||
} | ||
|
||
Promise.all([ | ||
onceAnEvent(), | ||
onceAnEventWithTwoArgs(), | ||
catchesErrors(), | ||
stopListeningAfterCatchingError(), | ||
onceError() | ||
]); |