I know Promise.all will wait all Promise resolved or one Promise rejected. how can I make it wait all Promise resolved or rejected?
Asked
Active
Viewed 92 times
0
-
[`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) _returns a promise that resolves after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise._ – blex Dec 14 '20 at 12:48
2 Answers
1
You are probably looking for Promise.allSettled()
.
The Promise.allSettled() method returns a promise that resolves after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.

Jax-p
- 7,225
- 4
- 28
- 58
0
You can define a custom function in this way. This function will always succeed without rejection.
const reflect = p => p.then(v => ({v, status: "fulfilled" }),
e => ({e, status: "rejected" }));
reflect(promise).then((v => {
console.log(v.status);
});
And then you can apply Promise.all
in this way, and can filter result depending on results of individual Promise.
Promise.all(arr.map(reflect)).then(function(results){
var success = results.filter(x => x.status === "fulfilled");
});

Prime
- 2,809
- 1
- 7
- 23