From 7cf6f9e964aa00772965391c23acda6d71972a9a Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 4 Jul 2019 16:20:20 -0400 Subject: [PATCH] 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 Reviewed-By: Masashi Hirano Reviewed-By: Ruben Bridgewater Reviewed-By: Rich Trott Reviewed-By: Anna Henningsen --- lib/internal/process/promises.js | 10 ++++++---- .../test-promise-handled-rejection-no-warning.js | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-promise-handled-rejection-no-warning.js diff --git a/lib/internal/process/promises.js b/lib/internal/process/promises.js index ee54594ba3c66b..ab833776070449 100644 --- a/lib/internal/process/promises.js +++ b/lib/internal/process/promises.js @@ -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; } diff --git a/test/parallel/test-promise-handled-rejection-no-warning.js b/test/parallel/test-promise-handled-rejection-no-warning.js new file mode 100644 index 00000000000000..14b06affb73825 --- /dev/null +++ b/test/parallel/test-promise-handled-rejection-no-warning.js @@ -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());