I'm very new to JS and functional programming in general and am struggling to find a graceful solution to this problem. Essentially, I want to make async requests to a MongoDB server, and return the results to an async to map function. The problem I am having in that the actual function within async.map is asynchronous itself. I would like to know a graceful solution here, or at least get a pointer in the right direction! Thanks!
async.map(subQuery,
function(item){
collection.distinct("author", item, function(err, authors){
counter++;
console.log("Finished query: " + counter);
var key = item['subreddit'];
return { key: authors };
})
},
function(err, result){
if (err)
console.log(err);
else{
console.log("Preparing to write to file...");
fs.writeFile("michaAggregate.json", result, function() {
console.log("The file was saved!");
});
}
db.close();
}
);