Skip to content

Instantly share code, notes, and snippets.

@hsubra89
Created August 13, 2014 05:00
Show Gist options
  • Save hsubra89/932cbc390244a763afce to your computer and use it in GitHub Desktop.
Save hsubra89/932cbc390244a763afce to your computer and use it in GitHub Desktop.
Promises Example 2
var allTheXmlsYay = [];
function getXML(url) {
var d = $q.defer();
d3.xml(url, 'image/svg+xml' function(xml) {
// If error variable is present
// if(err) d.reject(err);
d.resolve(xml);
});
return d.promise;
}
var promises = icons.map(function(icon) {
// This will return the promise associated with getting this icon
return getXML('someUrl' + icon);
});
// At this point, var promises is an array of promises for all your icons
$q.all(promises)
.then(function(XMLArray) {
//XMLArray is an array of all the resolved XML's
// Only continue on here once allTheXmlsYay is full
// So you might call the next continuation function here
}, function(err) {
// Comes here if any of the promises called d.reject();
// Can handle the error appropriately
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment