Wednesday, 19 April 2017

node.js - javascript callback function racing ahead

Folks, I have the following for loop, inside of which I call vehicles.alertsVid function from another module, which returns a status for each vehicle. The results do arrive, but later, and do not get added properly.



What is the proper way to write this, so the main for loop does not race ahead, and temp.status gets written in time? :)




for (var i = vehiclesStuff.length - 1; i >= 0; i--) {
var temp = {};
temp.name = vehiclesStuff[i].nickname.S;
temp.plate = vehiclesStuff[i].plate.S;
temp.base = vehiclesStuff[i].base.S;
temp.vin = vehiclesStuff[i].vin.S;
temp.vid = vehiclesStuff[i].vid.S;

var myfunc = function(t,results,done){return function(){
console.log ("ALERTS",results);

t.status = results.toString();
done();
}};

vehicles.alertsVid(temp.vid, myfunc(temp));

vehicleArray.push(temp);
};
callback()

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...