I need to filter an array, depending whether or not each element's sub-element, obtained by retrieving data from an external API, matches a criteria.
The code might be clearer than an explanation :
//Suppose there is an array filled
//_.filter(array, predicate)
var filtered_array = _.filter(array,function(element){
//Async operation
webservices.doStuff(element, function(sub_element){
//This return is meant for the filter, but returns on .doStuff callback
return sub_element.thing === "ok";
});
});
The problem here, is I don't know how to return the predicate's result here, as webservices.doStuff
is asynchronous!
Any idea?