@@ -884,6 +884,60 @@ ee.emit('error', new Error('boom'));
884
884
// Prints: ok boom
885
885
```
886
886
887
+ ### Awaiting multiple events emitted on ` process.nextTick() `
888
+
889
+ There is an edge case worth noting when using the ` events.once() ` function
890
+ to await multiple events emitted on in the same batch of ` process.nextTick() `
891
+ operations, or whenever multiple events are emitted synchronously. Specifically,
892
+ because the ` process.nextTick() ` queue is drained before the ` Promise ` microtask
893
+ queue, and because ` EventEmitter ` emits all events synchronously, it is possible
894
+ for ` events.once() ` to miss an event.
895
+
896
+ ``` js
897
+ const { EventEmitter , once } = require (' events' );
898
+
899
+ const myEE = new EventEmitter ();
900
+
901
+ async function foo () {
902
+ await once (myEE, ' bar' );
903
+ console .log (' bar' );
904
+
905
+ // This Promise will never resolve because the 'foo' event will
906
+ // have already been emitted before the Promise is created.
907
+ await once (myEE, ' foo' );
908
+ console .log (' foo' );
909
+ }
910
+
911
+ process .nextTick (() => {
912
+ myEE .emit (' bar' );
913
+ myEE .emit (' foo' );
914
+ });
915
+
916
+ foo ().then (() => console .log (' done' ));
917
+ ```
918
+
919
+ To catch both events, create each of the Promises * before* awaiting either
920
+ of them, then it becomes possible to use ` Promise.all() ` , ` Promise.race() ` ,
921
+ or ` Promise.allSettled() ` :
922
+
923
+ ``` js
924
+ const { EventEmitter , once } = require (' events' );
925
+
926
+ const myEE = new EventEmitter ();
927
+
928
+ async function foo () {
929
+ await Promise .all ([once (myEE, ' bar' ), once (myEE, ' foo' )]);
930
+ console .log (' foo' , ' bar' );
931
+ }
932
+
933
+ process .nextTick (() => {
934
+ myEE .emit (' bar' );
935
+ myEE .emit (' foo' );
936
+ });
937
+
938
+ foo ().then (() => console .log (' done' ));
939
+ ```
940
+
887
941
## ` events.captureRejections `
888
942
<!-- YAML
889
943
added: v12.16.0
0 commit comments