so I have the following code to loop through a Object:
for(var x in block){
sendTextMessage(block[x].text, sender, function(callback){
//increment for?
})
}
For each iteration I want to do a request (send a facebook message), only after that request has finished, I want to go through the next iteraction, this is because without any callbacks, the messages won't be sent in the right succession.
function sendTextMessage(text, sender, callback) {
let messageData = { text:text}
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:sender},
message: messageData,
}
}, function(error, response, body) {
if (response.statusCode >= 200 && response.statusCode < 300){
if(callback) callback('success')
}
})
}
I've had this problem before and not been able to solve it, how can I, somehow do this?
If you have any questions, please ask. Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…