Skip to content

Instantly share code, notes, and snippets.

@motss
Created December 18, 2015 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motss/9181af09784c5a5d8b37 to your computer and use it in GitHub Desktop.
Save motss/9181af09784c5a5d8b37 to your computer and use it in GitHub Desktop.
ES2015 Promise
var dates = [1, 2, 3];
var done = dates.map(function (date) {
return new Promise(function (resolve, reject) {
var json = ['a', 'b', 'c'];
if (date > Math.random() * 10) {
setTimeout(reject, Math.random() * 5000, date);
}else {
setTimeout(resolve, Math.random() * 5000, json[Math.ceil(Math.random() * 3) - 1]);
}
});
});
Promise.all(done).then(function (value) {
console.log('Success: ');
console.log(value);
}).catch(function (reason) {
console.log('Failed! Reason: ');
console.log(reason);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment