r/learnjavascript • u/Low_Mammoth_9371 • Feb 06 '23
Options for handling multiple promises
[removed] — view removed post
40
Upvotes
3
u/Low_Mammoth_9371 Feb 06 '23
Code to copy below, YT tutorial here: https://youtu.be/LE_L9OAh6Hk
const makeCoffee = new Promise((resolve, reject) => {
setTimeout(() => resolve("☕"), 200) });
const makeBreakfast = new Promise((resolve, reject) => {
setTimeout(() => resolve("🍳"), 800) });
const goToWork = new Promise((resolve, reject) => {
setTimeout(() => resolve("🏃♂️"), 2000) });
const promises = [makeCoffee, makeBreakfast, goToWork];
// Joint handling options (choose one):
Promise.all(promises).then(res => console.log(res));
// Logs result of all promises, if and when all successful
Promise.allSettled(promises).then(res => console.log(res));
// Logs results of all, even if one fails
Promise.any(promises).then(res => console.log(res));
// Logs result of fastest successful promise
Promise.race(promises).then(res => console.log(res));
// Logs result of fastest promise, even if it fails
promises.forEach(async promise => console.log(await promise));
// Logs results in order, as soon as each completes
•
u/kevinmrr Feb 06 '23
Post the original content, not a crosspost.