Created
December 18, 2015 16:05
-
-
Save motss/9181af09784c5a5d8b37 to your computer and use it in GitHub Desktop.
ES2015 Promise
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 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