process: refactor unhandledRejection logic

This commit prevents a deprecation warning from being emitted
if the unhandledRejection event was actually handled.

PR-URL: https://github.com/nodejs/node/pull/28540
Fixes: https://github.com/nodejs/node/issues/28539
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Masashi Hirano <shisama07@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
cjihrig 2019-07-04 16:20:20 -04:00
parent 327c6734cb
commit 7cf6f9e964
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
2 changed files with 14 additions and 4 deletions

View File

@ -199,10 +199,12 @@ function processPromiseRejections() {
}
case kDefaultUnhandledRejections: {
const handled = process.emit('unhandledRejection', reason, promise);
if (!handled) emitUnhandledRejectionWarning(uid, reason);
if (!deprecationWarned) {
emitDeprecationWarning();
deprecationWarned = true;
if (!handled) {
emitUnhandledRejectionWarning(uid, reason);
if (!deprecationWarned) {
emitDeprecationWarning();
deprecationWarned = true;
}
}
break;
}

View File

@ -0,0 +1,8 @@
'use strict';
const common = require('../common');
// This test verifies that DEP0018 does not occur when rejections are handled.
common.disableCrashOnUnhandledRejection();
process.on('warning', common.mustNotCall());
process.on('unhandledRejection', common.mustCall());
Promise.reject(new Error());