I would do this, which will iterate over each URL, creating a chain of promises that starts when the previous one completes, and solves with an array of query results.
return urls.reduce(function(acc, url){ return acc.then(function(results) return myfunction(url).then(function(requestResult){ return results.concat(requestResult) }); }); }, Q.resolve([]));
You can also include this in the helper:
var results = map(urls, myfunction); function map(items, fn){ return items.reduce(function(acc, item){ return acc.then(function(results) return fn(item).then(function(result){ return results.concat(result) }); }); }, Q.resolve([]) }
Please note: bluebird library has a helper to simplify this kind of thing.
return Bluebird.map(urls, myfunction, {concurrency: 1});
loganfsmyth
source share