Skip to content

Instantly share code, notes, and snippets.

@riston
Created June 19, 2015 12:58
Show Gist options
  • Save riston/e0f5c193142300e74408 to your computer and use it in GitHub Desktop.
Save riston/e0f5c193142300e74408 to your computer and use it in GitHub Desktop.
Promise and handling all
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