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