Last updated 2 years ago
Was this helpful?
JS ⟩ async ⟩ Promise ⟩ in parallel
Promise.all()
will reject immediately upon any of the input promises rejecting.
()
will wait for all input promises to complete, regardless of whether or not one rejects.
will always return the
await promises (in parallel or in series)
// rejected if any of the input Promises are rejected, // otherwise, fulfilled with an array of values of each Promise. Promise.all( // array of URLs -> array of Promises urls.map(url => fetch(url).then(response => response.text()) ).then( texts =>
💾 replit:Promise.all()
// custom Promise const promiseFoo = new Promise((resolve, reject) => { setTimeout(resolve, 2000, 'foo'); // ╰───╯ <---- parameter to `resolve` });
💾 replit:Promise.race()
// ⭐ Promise.race() // - settled immediately if any is settled. Promise.race([ new Promise(resolve => setTimeout(resolve, 500, 'one')), new Promise(resolve => setTimeout(resolve, 200, 'two')), // resolve faster ]).then(value => { console.log(value); // 'two' });
Promise.all() - rejected immediately if any rejetcted, fulfilled if all fulfilled.
Promise.allSettled() - a Promise that will always be fulfilled (with an array of outcome objects) when all settled.
Promise.race() - settled immediately if any is settled.
await promises
Promise.allSettled()