I am using waterline + sails for one api and i have the following function that has nested exec() calls.
AcademyService.getAll().exec(function (error, data) {
if (error) {
return res.json({result: false, message: error});
}
var academies = data;
for (var i = 0; i < Object.keys(academies).length; i++) {
var user_id;
user_id = academies[i].academy_user_id;
(function (index) {
UserService.getOneById(user_id).exec(function (error, data) {
if (error) {
academies[index].academy_user_id = null;
}
academies[index].academy_user_id = data.user_fname+" "+data.user_lname;
});
})(i);
}
return res.json({result: true, academies: academies});
});
Problem with above code is that, the last return statement does not have updated academies object. i.e. it does not have the updated value for its academy_user_id. I attribute this to the asynchronous behaviour of the calls and i think the when we reach the last return, the async calls for UserService are still running in the loop and updated academies object is not passed on.
Now, a hasty solution is to return the academies object from within the UserService.getOneById callback with a loop end check but that does not seem to be the right approach. So,can we somehow do the inner exec() synchronously?