Skip to content

Instantly share code, notes, and snippets.

@ilearnjavascript
Created March 27, 2019 23:28
Show Gist options
  • Save ilearnjavascript/a4119ad5885bfcd23236c6658d4b0049 to your computer and use it in GitHub Desktop.
Save ilearnjavascript/a4119ad5885bfcd23236c6658d4b0049 to your computer and use it in GitHub Desktop.
es6 - promises - 3.js
var sprinter1 = new Promise(function(resolve, reject) {
var name = "John"
setTimeout(function() {
resolve(name)
}, 3000);
});
var sprinter2 = new Promise(function(resolve, reject) {
var name = "Jack"
setTimeout(function() {
resolve(name)
}, 2000);
});
var sprinter3 = new Promise(function(resolve, reject) {
var name = "Joe"
setTimeout(function() {
resolve(name)
}, 1000);
});
var allReachGoal = function () {
Promise.all([sprinter1, sprinter2, sprinter3])
.then(function(name) {
console.log(name + ' reached their goal')
})
.catch(function(err) {
console.log('All failed because of ' + err)
});
}
allReachGoal();
// outputs: John,Jack,Joe reached their goal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment