-
-
Save ilearnjavascript/a4119ad5885bfcd23236c6658d4b0049 to your computer and use it in GitHub Desktop.
es6 - promises - 3.js
This file contains hidden or 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
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