2016-03-15 21:13:52 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const promiseRejectEvent = process._promiseRejectEvent;
|
|
|
|
const hasBeenNotifiedProperty = new WeakMap();
|
2016-04-28 10:11:09 +02:00
|
|
|
const promiseToGuidProperty = new WeakMap();
|
2016-03-15 21:13:52 +01:00
|
|
|
const pendingUnhandledRejections = [];
|
2016-04-28 10:11:09 +02:00
|
|
|
let lastPromiseId = 1;
|
2016-03-15 21:13:52 +01:00
|
|
|
|
|
|
|
exports.setup = setupPromises;
|
|
|
|
|
2016-11-09 02:04:04 +01:00
|
|
|
function getAsynchronousRejectionWarningObject(uid) {
|
|
|
|
return new Error('Promise rejection was handled ' +
|
|
|
|
`asynchronously (rejection id: ${uid})`);
|
|
|
|
}
|
|
|
|
|
2016-03-15 21:13:52 +01:00
|
|
|
function setupPromises(scheduleMicrotasks) {
|
|
|
|
process._setupPromises(function(event, promise, reason) {
|
|
|
|
if (event === promiseRejectEvent.unhandled)
|
|
|
|
unhandledRejection(promise, reason);
|
|
|
|
else if (event === promiseRejectEvent.handled)
|
|
|
|
rejectionHandled(promise);
|
|
|
|
else
|
2016-04-15 05:51:04 +02:00
|
|
|
require('assert').fail(null, null, 'unexpected PromiseRejectEvent');
|
2016-03-15 21:13:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
function unhandledRejection(promise, reason) {
|
|
|
|
hasBeenNotifiedProperty.set(promise, false);
|
2016-04-28 10:11:09 +02:00
|
|
|
promiseToGuidProperty.set(promise, lastPromiseId++);
|
2016-03-15 21:13:52 +01:00
|
|
|
addPendingUnhandledRejection(promise, reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
function rejectionHandled(promise) {
|
2016-09-17 12:59:40 +02:00
|
|
|
const hasBeenNotified = hasBeenNotifiedProperty.get(promise);
|
2016-03-15 21:13:52 +01:00
|
|
|
if (hasBeenNotified !== undefined) {
|
|
|
|
hasBeenNotifiedProperty.delete(promise);
|
2016-04-28 10:11:09 +02:00
|
|
|
const uid = promiseToGuidProperty.get(promise);
|
|
|
|
promiseToGuidProperty.delete(promise);
|
2016-03-15 21:13:52 +01:00
|
|
|
if (hasBeenNotified === true) {
|
2016-11-09 02:04:04 +01:00
|
|
|
let warning = null;
|
|
|
|
if (!process.listenerCount('rejectionHandled')) {
|
|
|
|
// Generate the warning object early to get a good stack trace.
|
|
|
|
warning = getAsynchronousRejectionWarningObject(uid);
|
|
|
|
}
|
2016-03-15 21:13:52 +01:00
|
|
|
process.nextTick(function() {
|
2016-04-28 10:11:09 +02:00
|
|
|
if (!process.emit('rejectionHandled', promise)) {
|
2016-11-09 02:04:04 +01:00
|
|
|
if (warning === null)
|
|
|
|
warning = getAsynchronousRejectionWarningObject(uid);
|
2016-04-28 10:11:09 +02:00
|
|
|
warning.name = 'PromiseRejectionHandledWarning';
|
|
|
|
warning.id = uid;
|
|
|
|
process.emitWarning(warning);
|
|
|
|
}
|
2016-03-15 21:13:52 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 03:28:20 +02:00
|
|
|
function emitWarning(uid, reason) {
|
|
|
|
const warning = new Error('Unhandled promise rejection ' +
|
|
|
|
`(rejection id: ${uid}): ${reason}`);
|
|
|
|
warning.name = 'UnhandledPromiseRejectionWarning';
|
|
|
|
warning.id = uid;
|
2016-11-09 02:04:04 +01:00
|
|
|
if (reason instanceof Error) {
|
|
|
|
warning.stack = reason.stack;
|
|
|
|
}
|
2016-09-18 03:28:20 +02:00
|
|
|
process.emitWarning(warning);
|
|
|
|
if (!deprecationWarned) {
|
|
|
|
deprecationWarned = true;
|
|
|
|
process.emitWarning(
|
|
|
|
'Unhandled promise rejections are deprecated. In the future, ' +
|
|
|
|
'promise rejections that are not handled will terminate the ' +
|
|
|
|
'Node.js process with a non-zero exit code.',
|
2016-12-04 21:47:01 +01:00
|
|
|
'DeprecationWarning', 'DEP0018');
|
2016-09-18 03:28:20 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 06:16:40 +02:00
|
|
|
var deprecationWarned = false;
|
2016-03-15 21:13:52 +01:00
|
|
|
function emitPendingUnhandledRejections() {
|
2016-04-28 10:11:09 +02:00
|
|
|
let hadListeners = false;
|
2016-03-15 21:13:52 +01:00
|
|
|
while (pendingUnhandledRejections.length > 0) {
|
2016-04-28 10:11:09 +02:00
|
|
|
const promise = pendingUnhandledRejections.shift();
|
|
|
|
const reason = pendingUnhandledRejections.shift();
|
2016-03-15 21:13:52 +01:00
|
|
|
if (hasBeenNotifiedProperty.get(promise) === false) {
|
|
|
|
hasBeenNotifiedProperty.set(promise, true);
|
2016-04-28 10:11:09 +02:00
|
|
|
const uid = promiseToGuidProperty.get(promise);
|
2016-03-15 21:13:52 +01:00
|
|
|
if (!process.emit('unhandledRejection', reason, promise)) {
|
2016-09-18 03:28:20 +02:00
|
|
|
emitWarning(uid, reason);
|
2016-03-15 21:13:52 +01:00
|
|
|
} else {
|
|
|
|
hadListeners = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hadListeners;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addPendingUnhandledRejection(promise, reason) {
|
|
|
|
pendingUnhandledRejections.push(promise, reason);
|
|
|
|
scheduleMicrotasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
return emitPendingUnhandledRejections;
|
|
|
|
}
|