In the following, code unhandledRejection
doesn't get fired for p2
, even though it also gets rejected, albeit later than p1
:
process.on('unhandledRejection', (reason, promise) =>
console.log(`unhandledRejection: ${reason}`));
async function delay(ms) {
await new Promise(r => setTimeout(r, ms));
}
const p1 = async function f1(){
await delay(100);
throw new Error("f1");
}();
const p2 = async function f2(){
await delay(200);
throw new Error("f2");
}();
try {
await Promise.race([p1, p2]);
//await Promise.race([p1]);
}
catch (e) {
console.error(e.message);
}
If I change the commented line like this:
//await Promise.race([p1, p2]);
await Promise.race([p1]);
... then unhandledRejection
does get fired for p2
, as expected. The same behavior is observed for Promise.all()
.
Thus, Promise.race
and Promise.all
effectively prevent the unhandledRejection
event for promises which don't win the race but still get rejected. Is it a documented behavior? I can't seem to find any mentions of that in the specs.
Description. The race function returns a Promise that is settled the same way (and takes the same value) as the first promise that settles amongst the promises of the iterable passed as an argument. If the iterable passed is empty, the promise returned will be forever pending.
The Promise. race() method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise.
A promise is an object that may produce a single value some time in the future : either a resolved value, or a reason that it's not resolved (e.g., a network error occurred). A promise may be in one of 3 possible states: fulfilled, rejected, or pending.
Assuming we have the processing power and that our promises can run in parallel, there is a hard limit of just over 2 million promises.
The Promise.all () returns a promise that resolves to an array of values from the input promises while the Promise.race () returns a promise that resolves to the value from the first settled promise. In the next article, I am going to discuss JavaScript Promise Error Handling with Examples.
The Promise object in Javascript offers built-in methods, such as Promise.all and Promise.race. Promise.all accepts an iterable of promises as input and returns a single Promise of the results of the input promises after attempting to fulfill all of them.
As you can understand from the example, the response is an array of the results of the input promises. Promise.all is used when you have multiple asynchronous operations that needs to be run in parallel, and once ALL of those operations are finished, you want to perform some operations.
For example, if we pass in two promises that resolve after a timeout and one promise that rejects immediately, then Promise.all () will reject immediately. It does not depend on if the other promises have resolved.
Yes, Promise.race
and Promise.all
"handle" the result of all of the promises you pass into them, regardless of whether that result was relevant to the settlement of the promise from race
/all
. So the "losing" promise in Promise.race
is still handled, even though the promise from Promise.race
only reflects what happened with the winning promise. Similarly, if Promise.all
rejects because one of its input promises rejects, any rejections from other input promises later are handled but nothing is done with them.
You can see this in the specification where it hooks up handlers to both fulfillment and rejection of each promise passed in, for instance in Step 3.i of PerformPromiseRace:
Perform ? Invoke(nextPromise, "then", « resultCapability.[[Resolve]], resultCapability.[[Reject]] »).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With