ohmybuck

Promise Arrays and Why It’s Important to Keep Tracking Them

July 26, 2021

I was battling to explain how difficult error handling can be when inside a promise array - here’s a fun example for either a tech test or workshop.

const someAsyncFunction = () => {
  const text = "still running"
  console.log(text)
  Promise.reject(text)
}

Promise.all([someAsyncFunction].map(fn => fn()))
  .then(console.log)
  .catch(console.log)

The solution

const someAsyncFunction = () => {
  const text = "still running"
  console.log(text)
  return Promise.reject(text) // we're now returning the promise.
}

Promise.all([someAsyncFunction].map(fn => fn()))
  .then(console.log)
  .catch(console.log)

Promise.allSettled is also worth considering.


Written by Oliver Smit. I love talking about web technologies, good design practices, optimising productivity, and rapid prototyping of projects. Check out my work on Github