forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromises.js
More file actions
76 lines (65 loc) · 2.33 KB
/
Copy pathpromises.js
File metadata and controls
76 lines (65 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
const promiseRejectEvent = process._promiseRejectEvent;
const hasBeenNotifiedProperty = new WeakMap();
const pendingUnhandledRejections = [];
const traceUnhandledRejection = process.traceUnhandledRejection;
const throwUnhandledRejection = process.throwUnhandledRejection;
const prefix = `(${process.release.name}:${process.pid}) `;
exports.setup = setupPromises;
function setupPromises(scheduleMicrotasks) {
process._setupPromises(function(event, promise, reason) {
if (event === promiseRejectEvent.unhandled)
unhandledRejection(promise, reason);
else if (event === promiseRejectEvent.handled)
rejectionHandled(promise);
else
require('assert').fail(null, null, 'unexpected PromiseRejectEvent');
});
function unhandledRejection(promise, reason) {
hasBeenNotifiedProperty.set(promise, false);
addPendingUnhandledRejection(promise, reason);
}
function rejectionHandled(promise) {
var hasBeenNotified = hasBeenNotifiedProperty.get(promise);
if (hasBeenNotified !== undefined) {
hasBeenNotifiedProperty.delete(promise);
if (hasBeenNotified === true) {
process.nextTick(function() {
process.emit('rejectionHandled', promise);
});
}
}
}
function emitPendingUnhandledRejections() {
var hadListeners = false;
while (pendingUnhandledRejections.length > 0) {
var promise = pendingUnhandledRejections.shift();
var reason = pendingUnhandledRejections.shift();
if (hasBeenNotifiedProperty.get(promise) === false) {
hasBeenNotifiedProperty.set(promise, true);
if (!process.emit('unhandledRejection', reason, promise)) {
// Nobody is listening.
// TODO(petkaantonov) Take some default action, see #830
if (traceUnhandledRejection) {
if (reason && reason.stack) {
console.error(`${prefix}${reason.stack}`);
} else {
console.error(`${prefix}${reason}`);
}
}
if (throwUnhandledRejection) {
throw reason;
}
} else {
hadListeners = true;
}
}
}
return hadListeners;
}
function addPendingUnhandledRejection(promise, reason) {
pendingUnhandledRejections.push(promise, reason);
scheduleMicrotasks();
}
return emitPendingUnhandledRejections;
}