JavaScript Loop Over Multiple Promises

node v17.9.1
version: 3.0.0
endpointsharetweet
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); } })();
Loading…

no comments

    sign in to comment