JavaScript Loop Over Promises with allSettled()

node v14.20.1
version: 1.0.0
endpointsharetweet
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)); })();
Loading…

no comments

    sign in to comment