What are arguments of promise.all.catch(e)
Is e an array of errors or object?
Promise.all([promise1, promise2, ..])
.then((result1, result2,..)=>{})
.catch((error1,error2..???)=>{
})
How can we get array of errors? if not Promise.all then how?
What are arguments of promise.all.catch(e)
Is e an array of errors or object?
Promise.all([promise1, promise2, ..])
.then((result1, result2,..)=>{})
.catch((error1,error2..???)=>{
})
How can we get array of errors? if not Promise.all then how?
As per spec
The all function returns a new promise which is fulfilled with an array of fulfillment values for the passed promises, or rejects with the reason of the first passed promise that rejects. It resolves all elements of the passed iterable to promises as it runs this algorithm.
Hence promise.catch
When the catch method is called with argument onRejected the following steps are taken:
- Let promise be the this value.
- Return Invoke(promise, "then", «undefined, onRejected»).
Hence reason of the first passed promise that rejects is the argument of promise.all.catch
.
There is no difference between an error handling of a simple Promise
and a Promise.all
.
Which means :
Promise.all([p1, p2, p3, ...])
.then((v) => {
// ...
})
.catch((err) => {
// ...
});
With one error in the catch.
Promise.all
return in an array (in the then) all results of given Promises
.
In case of error it throw the error, that you catch (1 error).
EDIT : About the fact to get all errors which seems to be the real question here
Javascript is single threaded, which means that it can execute only one function at a time. Javascript asynchronism means only that I/O won't be blocking.
The way Promise.all work is that at the first encountered error, it throw. And stop other promises. So you cannot have multiple errors.
What you might want to accomplish requires a different approach:
const promises = [p1, p2];
const promisesWithExceptionInfo = promises.map(p => {
return new Promise(resolve => {
p.then(result => resolve({result})).catch(error => resolve({error}));
});
});
// now your promises will always be resolved but contain either a result or an error
Promise.all(promisesWithExceptionInfo).then(res => {
// res is an array of objects where each promise result or error can be evaluated
});