Created
May 21, 2020 19:44
-
-
Save plaxdan/412280fca0105f435d6712b2f7c3fcac to your computer and use it in GitHub Desktop.
Promise.all vs Promise.allSettled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const first = new Promise(resolve => { | |
console.log("First finished."); | |
resolve(); | |
}); | |
const secondWithError = new Promise((resolve, reject) => reject("Second failed")); | |
const third = new Promise(resolve => { | |
setTimeout((() => { | |
console.log("Third finished"); | |
resolve(); | |
}), 3000) | |
}) // wait three seconds | |
const list = [first, secondWithError, third]; | |
const all = Promise.all(list); | |
try { | |
all.then(resolve => {s | |
console.log(">>>>> ALL FINISHED"); // This will never run!!! | |
resolve(); | |
}) | |
} catch(error) { | |
console.log("Error occured in Promise.all") | |
} | |
const allSettled = Promise.allSettled(list); | |
try { | |
allSettled.then(resolve => { | |
console.log(">>>>> ALL SETTLED"); | |
resolve(); | |
}) | |
} catch(error) { | |
console.log("Error occured in Promise.allSettled") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment