How to filter a list using lodash if the filter function is asynchronous
I am new to lodash and Javascript in general. I am using nodejs. I am using lodash's filter function to filter some content in my collection.
Here is a snippet
filteredrows = _.filter(rows, function(row, index){
//here I need to call some asynchronous function which checks the row
//the return value of this asynchronous function will determine whether to return true or false for the filter function.
});
My question is, how do I do this? using a closure? Is it possible to do this in a lodash filter function? Thanks in advance.
lodash may not be the best tool for this job. I recommend using async
.
https://github.com/caolan/async#filter
Example: fs.exists
is an asynchronous function that checks for file existence and calls a callback.
async.filter(['file1','file2','file3'], fs.exists, function(results){
// results now equals an array of the existing files
});
Lodash is not an asynchronous tool. It makes filtering information in real time fast. When you need to make a process asynchronous, you should use bluebird , Async , Native promises, or callbacks.
I think you should be using Lodash and Underscore, just to organize Objectdata in real time.