Created
February 3, 2016 17:29
-
-
Save juan-m-medina/7f39815d49e6f56e8f49 to your computer and use it in GitHub Desktop.
ES6 Promises
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
var oneSecondCall = function(resolve, reject) { | |
setTimeout(resolve, 1000, 1); | |
}; | |
var twoSecondCall = function(resolve, reject) { | |
setTimeout(resolve, 2000, 2); | |
}; | |
var threeSecondCall = function(resolve, reject) { | |
setTimeout(resolve, 3000, 3); | |
}; | |
var testPromise = new Promise (oneSecondCall); | |
var secondPromise = new Promise (twoSecondCall); | |
var thirdPromise = new Promise (threeSecondCall); | |
var errorPromise = new Promise(function(resolve, reject) { reject(3);}) | |
testPromise | |
.then(function(val) { console.log(`Done with ${val}`); }) | |
.catch(function(errors) { console.log(`Errors ${errors}`); }); | |
Promise.all([testPromise, secondPromise, thirdPromise]) | |
.then(function(values) { console.log(` All Promises done with values ${values}`); }) | |
.catch(function(errors) { console.log(`Errors ${errors}`); }); | |
Promise.race([testPromise, secondPromise, thirdPromise]) | |
.then(function(values) { console.log(` All Promises done with values ${values}`); }) | |
.catch(function(errors) { console.log(`Errors ${errors}`); }); | |
Promise.all([testPromise, secondPromise, thirdPromise, errorPromise]) | |
.then(function(values) { console.log(` All Promises done with values ${values}`); }) | |
.catch(function(errors) { console.log(`Errors ${errors}`); }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment