Skip to content

Instantly share code, notes, and snippets.

@bobdobbalina
Last active June 3, 2020 14:34
Show Gist options
  • Save bobdobbalina/892d36575d6f5111af0135cf970fdea2 to your computer and use it in GitHub Desktop.
Save bobdobbalina/892d36575d6f5111af0135cf970fdea2 to your computer and use it in GitHub Desktop.
Javascript: Wait until promises ar complete
const PromiseArray = [
Promise.resolve(100),
Promise.reject(null),
Promise.resolve("Data release"),
Promise.reject(new Error('Something went wrong'))];
Promise.all(PromiseArray)
.then(data => console.log('all resolved! here are the resolve values:', data))
.catch(err => console.log('got rejected! reason:', err))
// If you want to wait until all your promises are complete, regardless whether they get rejected or resolved.
const PromiseArray = [
Promise.resolve(100),
Promise.reject(null),
Promise.resolve("Data release"),
Promise.reject(new Error('Something went wrong'))];
Promise.allSettled(PromiseArray)
.then(res => {
console.log(res);
})
.catch(err => console.log(err));
//[
//{status: "fulfilled", value: 100},
//{status: "rejected", reason: null},
//{status: "fulfilled", value: "Data release"},
//{status: "rejected", reason: Error: Something went wrong ...}
//]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment