Created
June 19, 2015 12:58
-
-
Save riston/e0f5c193142300e74408 to your computer and use it in GitHub Desktop.
Promise and handling all
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 getProviderInfo = function (provider) | |
{ | |
return new Promise(function (resolve, reject) { | |
var time = ~~(Math.random() * 2000) + 2000; | |
// Make request and get the data, simulate request api, no fail in our case | |
window.setTimeout(function () { | |
return resolve({ data: provider + " success in " + time }); | |
}, time); | |
}); | |
}; | |
var providers = [ | |
getProviderInfo("facebook"), | |
getProviderInfo("google"), | |
getProviderInfo("linkedin"), | |
getProviderInfo("twitter"), | |
]; | |
var handleResult = function (result) | |
{ | |
console.log("All the promises are resolved now we have data"); | |
console.log("Result", result); | |
}; | |
Promise.all(providers).then(handleResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment