As per MDN,
The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will resolve when all of the input's promises have resolved, or if the input iterable contains no promises. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error.
Here is a code-snippet which doesn't catch the error as I expected it to as per above definition :-
const promise1 = Promise.resolve(3);
const promise2 = () => {throw new Error('random error')};
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'foo');
});
Promise.all([promise1, promise2(), promise3]).then((values) => {
console.log(values);
}).catch(e=>console.log('Error caught',e));
I know if I convert the promise2()
to return a Promise which rejects then it will work. But what about the non-promises line ? Is it incorrect or am I missing something ?
Update - I got the answer for this behavior. Just curious about what could be possible scenarios for non-promises throwing an error as per the definition ?
const input = [promise1, promise2(), promise3]; Promise.all(input)
Nothing to do withPromise.all
- adigaasync
in there:const proimse2 = async () => ...
- TKoL