JavaScript Loop Over Promises with allSettled()
const one = () => {
return new Promise((resolve) => {
setTimeout(() => resolve(2), 2000);
})
};
const two = () => {
return new Promise((resolve, reject) => {
setTimeout(() => reject(3), 3000);
})
};
const three = () => {
return new Promise((resolve) => {
setTimeout(() => resolve(5), 5000);
})
};
// This async IFFE is only needed in NodeJS
(async () => {
const promisesArr = [one(), two(), three()];
const allPromises = await Promise.allSettled(promisesArr).then((promises) => {
for (const result of promises) {
console.log('result:', result)
}
}, (error) => console.error(error));
})();
no comments