Skip to content

Commit 81b42d2

Browse files
apapirovskitargos
authored andcommitted
process: emit unhandled warning immediately
PR-URL: #24632 Fixes: #24209 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
1 parent 9d54555 commit 81b42d2

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

lib/internal/process/promises.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function emitPromiseRejectionWarnings() {
108108
}
109109
}
110110

111-
let hadListeners = false;
111+
let maybeScheduledTicks = false;
112112
let len = pendingUnhandledRejections.length;
113113
while (len--) {
114114
const promise = pendingUnhandledRejections.shift();
@@ -118,10 +118,9 @@ function emitPromiseRejectionWarnings() {
118118
const { reason, uid } = promiseInfo;
119119
if (!process.emit('unhandledRejection', reason, promise)) {
120120
emitWarning(uid, reason);
121-
} else {
122-
hadListeners = true;
123121
}
122+
maybeScheduledTicks = true;
124123
}
125124
}
126-
return hadListeners || pendingUnhandledRejections.length !== 0;
125+
return maybeScheduledTicks || pendingUnhandledRejections.length !== 0;
127126
}

test/parallel/test-promises-unhandled-rejections.js

+19
Original file line numberDiff line numberDiff line change
@@ -699,3 +699,22 @@ asyncTest('Rejected promise inside unhandledRejection allows nextTick loop' +
699699
process.nextTick(() => promise.catch(() => done()));
700700
});
701701
});
702+
703+
asyncTest(
704+
'Unhandled promise rejection emits a warning immediately',
705+
function(done) {
706+
clean();
707+
Promise.reject(0);
708+
const { emitWarning } = process;
709+
process.emitWarning = common.mustCall((...args) => {
710+
if (timer) {
711+
clearTimeout(timer);
712+
timer = null;
713+
done();
714+
}
715+
emitWarning(...args);
716+
}, 2);
717+
718+
let timer = setTimeout(common.mustNotCall(), 10000);
719+
},
720+
);

0 commit comments

Comments
 (0)