diff --git a/lib/internal/process/promises.js b/lib/internal/process/promises.js index 031997fc1b3467..9814f9b51a3a3f 100644 --- a/lib/internal/process/promises.js +++ b/lib/internal/process/promises.js @@ -126,7 +126,7 @@ function handledRejection(promise) { } const unhandledRejectionErrName = 'UnhandledPromiseRejectionWarning'; -function emitWarning(uid, reason) { +function emitUnhandledRejectionWarning(uid, reason) { const warning = getError( unhandledRejectionErrName, 'Unhandled promise rejection. This error originated either by ' + @@ -144,20 +144,15 @@ function emitWarning(uid, reason) { } catch {} process.emitWarning(warning); - emitDeprecationWarning(); } let deprecationWarned = false; function emitDeprecationWarning() { - if (unhandledRejectionsMode === kDefaultUnhandledRejections && - !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.', - 'DeprecationWarning', 'DEP0018'); - } + 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.', + 'DeprecationWarning', 'DEP0018'); } // If this method returns true, we've executed user code or triggered @@ -186,7 +181,7 @@ function processPromiseRejections() { case kThrowUnhandledRejections: { fatalException(reason); const handled = process.emit('unhandledRejection', reason, promise); - if (!handled) emitWarning(uid, reason); + if (!handled) emitUnhandledRejectionWarning(uid, reason); break; } case kIgnoreUnhandledRejections: { @@ -195,12 +190,16 @@ function processPromiseRejections() { } case kAlwaysWarnUnhandledRejections: { process.emit('unhandledRejection', reason, promise); - emitWarning(uid, reason); + emitUnhandledRejectionWarning(uid, reason); break; } case kDefaultUnhandledRejections: { const handled = process.emit('unhandledRejection', reason, promise); - if (!handled) emitWarning(uid, reason); + if (!handled) emitUnhandledRejectionWarning(uid, reason); + if (!deprecationWarned) { + emitDeprecationWarning(); + deprecationWarned = true; + } break; } }