Skip to content

Instantly share code, notes, and snippets.

@plaxdan
Created May 21, 2020 19:44
Show Gist options
  • Save plaxdan/412280fca0105f435d6712b2f7c3fcac to your computer and use it in GitHub Desktop.
Save plaxdan/412280fca0105f435d6712b2f7c3fcac to your computer and use it in GitHub Desktop.
Promise.all vs Promise.allSettled
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