|
2 | 2 |
|
3 | 3 | const promiseRejectEvent = process._promiseRejectEvent; |
4 | 4 | const hasBeenNotifiedProperty = new WeakMap(); |
5 | | -const promiseToGuidProperty = new WeakMap(); |
6 | 5 | const pendingUnhandledRejections = []; |
7 | | -let lastPromiseId = 1; |
8 | 6 |
|
9 | 7 | exports.setup = setupPromises; |
10 | 8 |
|
11 | | -function getAsynchronousRejectionWarningObject(uid) { |
12 | | - return new Error('Promise rejection was handled ' + |
13 | | - `asynchronously (rejection id: ${uid})`); |
14 | | -} |
15 | | - |
16 | 9 | function setupPromises(scheduleMicrotasks) { |
| 10 | + const promiseInternals = {}; |
| 11 | + |
17 | 12 | process._setupPromises(function(event, promise, reason) { |
18 | 13 | if (event === promiseRejectEvent.unhandled) |
19 | 14 | unhandledRejection(promise, reason); |
20 | 15 | else if (event === promiseRejectEvent.handled) |
21 | 16 | rejectionHandled(promise); |
22 | 17 | else |
23 | 18 | require('assert').fail(null, null, 'unexpected PromiseRejectEvent'); |
24 | | - }); |
| 19 | + }, function getPromiseReason(data) { |
| 20 | + return data[data.indexOf('[[PromiseValue]]') + 1]; |
| 21 | + }, promiseInternals); |
25 | 22 |
|
26 | 23 | function unhandledRejection(promise, reason) { |
27 | 24 | hasBeenNotifiedProperty.set(promise, false); |
28 | | - promiseToGuidProperty.set(promise, lastPromiseId++); |
29 | 25 | addPendingUnhandledRejection(promise, reason); |
30 | 26 | } |
31 | 27 |
|
32 | 28 | function rejectionHandled(promise) { |
33 | 29 | const hasBeenNotified = hasBeenNotifiedProperty.get(promise); |
34 | 30 | if (hasBeenNotified !== undefined) { |
35 | 31 | hasBeenNotifiedProperty.delete(promise); |
36 | | - const uid = promiseToGuidProperty.get(promise); |
37 | | - promiseToGuidProperty.delete(promise); |
38 | 32 | if (hasBeenNotified === true) { |
39 | | - let warning = null; |
40 | | - if (!process.listenerCount('rejectionHandled')) { |
41 | | - // Generate the warning object early to get a good stack trace. |
42 | | - warning = getAsynchronousRejectionWarningObject(uid); |
43 | | - } |
| 33 | + promiseInternals.untrackPromise(promise); |
44 | 34 | process.nextTick(function() { |
45 | | - if (!process.emit('rejectionHandled', promise)) { |
46 | | - if (warning === null) |
47 | | - warning = getAsynchronousRejectionWarningObject(uid); |
48 | | - warning.name = 'PromiseRejectionHandledWarning'; |
49 | | - warning.id = uid; |
50 | | - process.emitWarning(warning); |
51 | | - } |
| 35 | + process.emit('rejectionHandled', promise); |
52 | 36 | }); |
53 | 37 | } |
54 | 38 |
|
55 | 39 | } |
56 | 40 | } |
57 | 41 |
|
58 | | - function emitWarning(uid, reason) { |
59 | | - const warning = new Error('Unhandled promise rejection ' + |
60 | | - `(rejection id: ${uid}): ${reason}`); |
61 | | - warning.name = 'UnhandledPromiseRejectionWarning'; |
62 | | - warning.id = uid; |
63 | | - if (reason instanceof Error) { |
64 | | - warning.stack = reason.stack; |
65 | | - } |
66 | | - process.emitWarning(warning); |
67 | | - if (!deprecationWarned) { |
68 | | - deprecationWarned = true; |
69 | | - process.emitWarning( |
70 | | - 'Unhandled promise rejections are deprecated. In the future, ' + |
71 | | - 'promise rejections that are not handled will terminate the ' + |
72 | | - 'Node.js process with a non-zero exit code.', |
73 | | - 'DeprecationWarning', 'DEP0018'); |
74 | | - } |
75 | | - } |
76 | | - var deprecationWarned = false; |
77 | 42 | function emitPendingUnhandledRejections() { |
78 | 43 | let hadListeners = false; |
79 | 44 | while (pendingUnhandledRejections.length > 0) { |
80 | 45 | const promise = pendingUnhandledRejections.shift(); |
81 | 46 | const reason = pendingUnhandledRejections.shift(); |
82 | 47 | if (hasBeenNotifiedProperty.get(promise) === false) { |
83 | 48 | hasBeenNotifiedProperty.set(promise, true); |
84 | | - const uid = promiseToGuidProperty.get(promise); |
85 | 49 | if (!process.emit('unhandledRejection', reason, promise)) { |
86 | | - emitWarning(uid, reason); |
| 50 | + promiseInternals.trackPromise(promise); |
87 | 51 | } else { |
88 | 52 | hadListeners = true; |
89 | 53 | } |
|
0 commit comments