Created
August 13, 2014 05:00
-
-
Save hsubra89/932cbc390244a763afce to your computer and use it in GitHub Desktop.
Promises Example 2
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 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