我正在使用javascript来使用facebook发送api。在javascript中运行一个接一个的函数
function sendmessage(callback) {
for (i = 0; i < recipientId.length; i++) {
var messageData = {
recipient: {
id: recipientId[i]
},
message: {
text: messageText
}
};
callSendAPI(messageData, pagetoken, id_notsent);
}
return callback();
}
function sendstatus() {
if (id_notsent.length == 0) {
res.statusCode = 200;
res.message = "Successfully sent generic message to all recipients";
} else {
res.statusCode = 400;
res.message = "Unable to send message to all users. Message not sent to recipients : " + id_notsent.toString();
};
resp.send(res);
}
sendmessage(sendstatus);
什么,我要做的是更新SendMessage函数将基本上包含用户ID correspoding到消息无法发送,然后相应地发回的响应使用sendstatus函数内部的id_notsent变量。但问题在于sendSessage中的回调在callSendAPI函数完成之前被调用。
其实我没有发送任何从callSendAPI函数返回。那么如何在这种情况下进一步采用这种解决方案呢? –
'callSendAPI'必须返回一个Promise或者提供一个回调参数。如果不这样做,则无法知道何时完成对服务器的异步调用 –