2
2
3
3
const promiseRejectEvent = process . _promiseRejectEvent ;
4
4
const hasBeenNotifiedProperty = new WeakMap ( ) ;
5
+ const promiseToGuidProperty = new WeakMap ( ) ;
5
6
const pendingUnhandledRejections = [ ] ;
7
+ let lastPromiseId = 1 ;
6
8
7
9
exports . setup = setupPromises ;
8
10
@@ -18,32 +20,45 @@ function setupPromises(scheduleMicrotasks) {
18
20
19
21
function unhandledRejection ( promise , reason ) {
20
22
hasBeenNotifiedProperty . set ( promise , false ) ;
23
+ promiseToGuidProperty . set ( promise , lastPromiseId ++ ) ;
21
24
addPendingUnhandledRejection ( promise , reason ) ;
22
25
}
23
26
24
27
function rejectionHandled ( promise ) {
25
28
var hasBeenNotified = hasBeenNotifiedProperty . get ( promise ) ;
26
29
if ( hasBeenNotified !== undefined ) {
27
30
hasBeenNotifiedProperty . delete ( promise ) ;
31
+ const uid = promiseToGuidProperty . get ( promise ) ;
32
+ promiseToGuidProperty . delete ( promise ) ;
28
33
if ( hasBeenNotified === true ) {
29
34
process . nextTick ( function ( ) {
30
- process . emit ( 'rejectionHandled' , promise ) ;
35
+ if ( ! process . emit ( 'rejectionHandled' , promise ) ) {
36
+ const warning = new Error ( 'Promise rejection was handled ' +
37
+ `asynchronously (rejection id: ${ uid } )` ) ;
38
+ warning . name = 'PromiseRejectionHandledWarning' ;
39
+ warning . id = uid ;
40
+ process . emitWarning ( warning ) ;
41
+ }
31
42
} ) ;
32
43
}
33
44
34
45
}
35
46
}
36
47
37
48
function emitPendingUnhandledRejections ( ) {
38
- var hadListeners = false ;
49
+ let hadListeners = false ;
39
50
while ( pendingUnhandledRejections . length > 0 ) {
40
- var promise = pendingUnhandledRejections . shift ( ) ;
41
- var reason = pendingUnhandledRejections . shift ( ) ;
51
+ const promise = pendingUnhandledRejections . shift ( ) ;
52
+ const reason = pendingUnhandledRejections . shift ( ) ;
42
53
if ( hasBeenNotifiedProperty . get ( promise ) === false ) {
43
54
hasBeenNotifiedProperty . set ( promise , true ) ;
55
+ const uid = promiseToGuidProperty . get ( promise ) ;
44
56
if ( ! process . emit ( 'unhandledRejection' , reason , promise ) ) {
45
- // Nobody is listening.
46
- // TODO(petkaantonov) Take some default action, see #830
57
+ const warning = new Error ( 'Unhandled promise rejection ' +
58
+ `(rejection id: ${ uid } ): ${ reason } ` ) ;
59
+ warning . name = 'UnhandledPromiseRejectionWarning' ;
60
+ warning . id = uid ;
61
+ process . emitWarning ( warning ) ;
47
62
} else {
48
63
hadListeners = true ;
49
64
}
0 commit comments