Live data from Hacker News

JavaScript Promise.all vs. Promise.allSettled

blog.jonlu.ca

11–15 of 15 posts

Re: JavaScript Promise.all vs. Promise.allSettled

#11
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".

Re: JavaScript Promise.all vs. Promise.allSettled

#12
post #11

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".

Author's point is that Promise.allSettled never rejects

Re: JavaScript Promise.all vs. Promise.allSettled

#15

allSettled is quite new (it isn't in the version of node packaged with LTS Ubuntu). How does one implement the same with all?

Something like this should work:

  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);
Post reply on HN