I would use Q or async to do this. Consider following example, you wrap each web service call in a promise and define another promise that will be resolved when all promises are resolved. Then you filter results ( ie array of all returns from all calls) according to your logic.
var source = [{a : 1}, {a:2}...] // your data
var all = Q.all(source.map((o) => Q.Promise((resolve, reject,notify)
=> webService.doStuff(o, resolve, reject))))
all.then((ret) => _.filter(ret.data, (elem) => elem.thing === "ok"))
.then((filtered) => ... // do something with filtered )
Most likely your web service proxy already have binding with Q or another promise library. So your code is going to be simpler, something like:
var all = Q.all(source.map(webService.doStuff))
.then(filter).then(process)
Or at least you can refactor it to be that simple.
Overall due to non-blocking-accept-callback-and-go nature of node.js environment, it is essential to master one promise library or another, async
and Q
are two most popular to the best of my knowledge.
(Update: right now it seems to be bluebird, and it is fastest too.)
Having either of them allow you to come up with much easier design when dealing with async calls.