I bet I have a common problem, but as I am still new to async javascript I am not sure what the (best) solution for my problem is.
I go through a for loop and in that loop i call a method including a callback method. In the callback method I want to know which loop is the latest one. But the method takes a little bit to proceed and at the time when it is completed once, the for loop around it is finished and the counter variable i is already at highest. So my question is, how can I determine the latest loop in the callback method?
Here is example code for it.
var i;
for (i = 0; i < numMessages; i++){
var lastMessage;
if (i+1 === numMessages) {
lastMessage = true;
}
else {
lastMessage = false;
}
twilioClient.sendMessage({
to: recipientNumber,
from: zenyaTwilioNumber,
body: (numMessages > 1? (i+1) + '/' + Math.ceil(options.messageBody.length/153) + ': ' + options.messageBody.substring(i*153, (i+1)*153): options.messageBody)
},
function (err, res) {
if (err) {
callback({error: err});
}
else {
console.log('Message:' +i);
console.log('numMessages: ' + numMessages); // Even after method is executed once, the lastMessage variable is already true
if (lastMessage) {
var returnObj = {
status : status,
recipient : res.to,
statusMessage : res.status
};
callback({result: returnObj}); // The callback should only be called once here.
}
}
});
}
Looking forward to your answers and learning best practice about how to achieve that.
Thanks, benni