If you have control over the alertsVid
function that is called from vehicles you may like to pass it the whole temp object and let it handle that instead of temp.vid
(where does this property come from anyway?).
This way, you are passing the same object reference so that when it is finally processed, you are adding the status to the right temp
(rightTemp
). That way it can return the right temp
as an argument in the callBack.
You are initializing a new object temp
, and adding name
and plate
. I don't see vid
as a property on temp
?
Also if you want to wait until all of your temp
s are processed and with a status BEFORE you call your final callback you can use a counter to wait for it to fire. If you don't want to do this, just ignore those lines... (I've commented with "// callBack handling
")
Also... fix those semicolons. :P
var numProcessed = 0; // callBack handling
var numToProcess = vehiclesStuff.length; // callBack handling
for (var i = vehiclesStuff.length - 1; i >= 0; i--) {
var temp = {};
temp.name = vehiclesStuff[i].nickname.S;
temp.plate = vehiclesStuff[i].plate.S;
vehicles.alertsVid(temp, function (rightTemp, results, done) {
console.log ("ALERTS",results);
rightTemp.status = results.toString();
done();
processedTemp(); // callBack handling
})
vehicleArray.push(temp);
}
// callBack handling
function processedTemp(){
numProcessed++;
if(numProcessed === numToProcess) callBack();
}
your vehicles.alertsVid function would then look like:
vehicles.alertsVid = function(temp, callBack){
...
callBack(temp, results, done); // using the same temp (object reference) that was passed in!!!
};
This is all assuming that you have control over the vehicles.alertsVid
function.