diff --git a/lib/internal/process/promises.js b/lib/internal/process/promises.js index 56a9f287a0a86e..da5581d650ab04 100644 --- a/lib/internal/process/promises.js +++ b/lib/internal/process/promises.js @@ -281,7 +281,7 @@ function strictUnhandledRejectionsMode(promise, promiseInfo, promiseAsyncId) { reason : new UnhandledPromiseRejection(reason); // This destroys the async stack, don't clear it after triggerUncaughtException(err, true /* fromPromise */); - if (promiseAsyncId === undefined) { + if (promiseAsyncId !== undefined) { pushAsyncContext( promise[kAsyncIdSymbol], promise[kTriggerAsyncIdSymbol], diff --git a/test/parallel/test-promise-unhandled-error-with-reading-file.js b/test/parallel/test-promise-unhandled-error-with-reading-file.js new file mode 100644 index 00000000000000..5a037eec7ca229 --- /dev/null +++ b/test/parallel/test-promise-unhandled-error-with-reading-file.js @@ -0,0 +1,29 @@ +// Flags: --unhandled-rejections=strict +'use strict'; + +const common = require('../common'); +const fs = require('fs'); +const assert = require('assert'); + +process.on('unhandledRejection', common.mustNotCall); + +process.on('uncaughtException', common.mustCall((err) => { + assert.ok(err.message.includes('foo')); +})); + + +async function readFile() { + return fs.promises.readFile(__filename); +} + +async function crash() { + throw new Error('foo'); +} + + +async function main() { + crash(); + readFile(); +} + +main();