大家好,我有以下循环,其中我调用另一个模块的vehicles.alertsVid
函数,该函数返回每个车辆的状态。结果确实到达了,但是较晚,并且没有被正确添加。
请问应该如何编写代码,以便主循环不会超前,并且及时写入temp.status
? :)
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()
vehicles.alertsVid()
触发了一个异步过程,因此您传递给它的函数在整个for循环完成之后以及callback()
返回之后才会被调用。@elclanrs - 是的,我知道。我只是在开玩笑(如果你能想象得到)。 - nnnnnn