I am using the async module to execute a set of function in parallel. However i am using a return value of another function in the callback of on of these function. But the problem is, function returns as undefined as the callback does not wait for it to be evaluated.
function getGenresId(genres){
var genresArr = parseCSV(genres);
Genre.find({name: {$in: genresArr } }, function (err, genres) {
var genresId = genres.map(function (genre) {
return genre.id;
});
return genresId.toString();
});
}
async.parallel({
getGenres: function (callback) {
if(checkempty(req.query.genres) === true){
callback(null, getGenresId(req.query.genres)});
}
else{
callback(null, '');
}
},
getActor: function (outercallback) {
if(checkempty(req.query.actors) === true) {
//other code here
}
else{
outercallback(null, '');
}
}
);
I do understand that the function getGenresId() contains a blocking call to the database and Node.js will process it asynchronously. But is there a way I could force the
callback(null, getGenresId(req.query.genres)});
to evaluate the getGenresId function and wait for it to return a value before proceeding. I could use async's series function here but is there a native/better way to do this ?