Node: unhandledRejection fails to occur if `.catch()` is used on the last promise

Created on 13 Dec 2019  路  2Comments  路  Source: nodejs/node

  • Version: 12.13.1 / 13.3.0
  • Platform: Linux lt2.cfware.com 5.3.11-200.fc30.x86_64 #1 SMP Tue Nov 12 19:25:25 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Take the following script:

'use strict';

Promise.reject('rejection 1');
Promise.reject('rejection 2').catch(() => {});

The expected result is an unhandledRejection for rejection 1. This script produces no warnings on node.js 12 or 13.

confirmed-bug promises

Most helpful comment

Something like this might help?

diff --git a/lib/internal/process/promises.js b/lib/internal/process/promises.js
index 35ebb39115f3..7cd40f96cc98 100644
--- a/lib/internal/process/promises.js
+++ b/lib/internal/process/promises.js
@@ -125,7 +125,8 @@ function handledRejection(promise) {
       return;
     }
   }
-  setHasRejectionToWarn(false);
+  if (maybeUnhandledPromises.size === 0 && asyncHandledRejections.length === 0)
+    setHasRejectionToWarn(false);
 }

 const unhandledRejectionErrName = 'UnhandledPromiseRejectionWarning';

@nodejs/promises-debugging @joyeecheung (I think this might have come from bf566718b29a6fae8cef8d0fecd7e77948726d5a)

All 2 comments

Something like this might help?

diff --git a/lib/internal/process/promises.js b/lib/internal/process/promises.js
index 35ebb39115f3..7cd40f96cc98 100644
--- a/lib/internal/process/promises.js
+++ b/lib/internal/process/promises.js
@@ -125,7 +125,8 @@ function handledRejection(promise) {
       return;
     }
   }
-  setHasRejectionToWarn(false);
+  if (maybeUnhandledPromises.size === 0 && asyncHandledRejections.length === 0)
+    setHasRejectionToWarn(false);
 }

 const unhandledRejectionErrName = 'UnhandledPromiseRejectionWarning';

@nodejs/promises-debugging @joyeecheung (I think this might have come from bf566718b29a6fae8cef8d0fecd7e77948726d5a)

@addaleax Yeah, that鈥檚 likely, as the PR only passed whatever test we had and this does not seem to be tested by us.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jmichae3 picture jmichae3  路  3Comments

sandeepks1 picture sandeepks1  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments

seishun picture seishun  路  3Comments

Brekmister picture Brekmister  路  3Comments