Promise Arrays and Why It’s Important to Keep Tracking Them
Updated May 26, 2024Created 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.