forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 5
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 <[email protected]> PR-URL: nodejs#26078 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]>
- Loading branch information
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() | ||
]); |