> Promise.allSettled can never throw
Of course not. And Promise.all neither. Promises don't throw, they reject.
async/await is syntactic sugar on top of that, which can make it look similar to throwing, but you will never actually "throw".
11–15 of 15 posts
> Promise.allSettled can never throw
Of course not. And Promise.all neither. Promises don't throw, they reject.
async/await is syntactic sugar on top of that, which can make it look similar to throwing, but you will never actually "throw".
The author seems to be a bit confused about the difference between "throwing" and "rejecting". > Promise.allSettled can never throw Of course not. And Promise.all neither. Promises don't throw, they reject. async/await is syntactic sugar on top of that, which can make it look similar to throwing, but you will never actually "throw".
How does one implement the same with all?
allSettled is quite new (it isn't in the version of node packaged with LTS Ubuntu). How does one implement the same with all?
function allSettled(promises) {
let settled = promises.map(p => p.then(
v => ({status: 'fulfilled', value: v}),
r => ({status: 'rejected', reason: r})
));
return Promise.all(settled);
}
let p1 = Promise.resolve('foo');
let p2 = Promise.resolve('bar');
let p3 = Promise.resolve('baz');
allSettled([p1, p2, p3]).then(console.log);
let p4 = Promise.reject('err1');
let p5 = Promise.resolve('bar');
let p6 = Promise.reject('err3');
allSettled([p4, p5, p6]).then(console.log);