I'm trying to loop through objects and call a method ("search") which is async and has a callback and when it is done with all objects, write the result object which is resultSet in a json file; but now the problem is since my search function is async writefile does not wait for them to finish and then write resultset to a json file, so it is writing empty objects to a json file; also it is worth mentioning I don't want to use promises; could you please let me know apart from promises what is the best approach to cope with this problem?
router.post('/export', function (req, res) {
Object.keys(models).forEach(function (name) {
resultSet[name] = [];
search(param1, name, function (err, data) {
if (err) {
console.error(req.host, req.path, err, err.stack);
res.json(400, err);
} else {
resultSet[name] = data;
}
});
});
var file = "myFile.json";
jf.writeFile(file,resultSet,function(err){
console.log(err);
});
});
async.mapdoes a great job as an asynchronousforEachequivalent.