Normally, when a Promise in JavaScript rejects without handling, we get unhandled promise rejection
error.
But then what happens to all rejected promises ignored by Promise.race
logic? Why don't they throw the same error?
Consider the following test:
const normal = new Promise((resolve, reject) => {
setTimeout(() => resolve(123), 100);
});
const err = new Promise((resolve, reject) => {
setTimeout(() => reject('ops'), 500);
});
const test = Promise.race([normal, err]);
test.then(data => {
console.log(data);
});
The test above simply outputs 123
, but no unhandled promise rejection
error for our err
promise.
I'm trying to understand what happens to all those rejected promises then, hence the question.
We potentially end up with a bunch of loose promises that continue running in the background, without any error handling, and never any reporting about unhandled promise rejections. This seems kind of dangerous.
Case in point. I was trying to implement combine
logic for asynchronous iterables (similar to this), which requires use of Promise.race
, while at the same time tracking rejections of any parameters passed into it, because the combine
function needs then to reject on the next request.