MDN URL: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race
The example does not display the difference with Promise.any()
The example
The difference with Promise.any()
I suggest to put this example for Promise.race():
`
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 500, 'one');
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(reject, 100, 'two');
});
Promise.race([promise1, promise2]).then((value) => {
console.log('succeeded with value:', value);
}).catch((reason) => {
console.log('failed with reason:', reason);
// Only promise1 is fulfilled, but promise2 is faster
});
// expected output: "failed with reason: two"
`
And this example for Promise.any():
`
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 500, 'one');
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(reject, 100, 'two');
});
Promise.any([promise1, promise2]).then((value) => {
console.log('succeeded with value:', value);
// Only promise1 is fulfilled, even though promise2 settled sooner
}).catch((reason) => {
console.log('failed with reason:', reason);
});
// expected output: "succeeded with value: one"
`
I tested both examples on Firefox devtools' console.
MDN Content page report details
en-us/web/javascript/reference/global_objects/promise/raceI want to take on this issue.
@keiya01 assigned! Thank you for your offer of help, and please feel free to ask if you have any questions.
Most helpful comment
Fixed: https://github.com/mdn/content/pull/4432