Last active
August 29, 2015 14:19
-
-
Save chinmay185/035a07cc0234a75ece03 to your computer and use it in GitHub Desktop.
Run async operation for each value in a collection where each async operation is executed only when the previous one completes.
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 Promise = require("bluebird"); | |
var apiUrls = ["url1", "url2", "url3"]; | |
var request = function(url) { | |
// request the rate limited api (returns a promise) | |
}; | |
var saveResponse = function(response) { | |
// save response to file/db (returns a promise) | |
}; | |
apiUrls | |
.reduce(function (accumulatedPromise, url) { | |
return accumulatedPromise | |
.then(function () { | |
return request(url); | |
}) | |
.then(saveResponse); | |
}, Promise.resolve()) | |
.then(function () { | |
console.log("all done."); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment