I'm fairly new to promises and have a problem wrapping my head around the Q.when()
chain.
I want to execute my promises sequentially.
This is what I've got so far. It's running sequential but it's not resolving my URLs. parseUrls(urls)
should return an array of all parseUrl(url)
resolutions.
function parseUrl(url) {
return function () {
var deferred = Q.defer();
// setTimeout as async placeholder
setTimeout(function() {
var result = {url: url};
deferred.resolve(result);
}
return deferred.promise;
}
}
function parseUrls(urls) {
var deferred = Q.defer();
var chain = Q.when();
urls.forEach(function (url) {
chain = chain.then(parseUrl(url));
});
chain = chain.then(deferred.resolve);
return deferred.promise;
}
getAllUrls()
.then(parseUrls)
.then(parseCards);