Skip to content

Instantly share code, notes, and snippets.

@juan-m-medina
Created February 3, 2016 17:29
Show Gist options
  • Save juan-m-medina/7f39815d49e6f56e8f49 to your computer and use it in GitHub Desktop.
Save juan-m-medina/7f39815d49e6f56e8f49 to your computer and use it in GitHub Desktop.
ES6 Promises
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