![](/img/trans.png)
[英]How to deal with an if statement using promises or synchronous calls in node.js
[英]How to sequence async calls with Promises in Node.js?
我是Java的新手,无法理解如何让我的函数相互运行。 我想使用Promise实现这一目标。
我正在按照Facebook Messenger教程制作聊天机器人。 基本上,我想一个接一个地发送消息。
如果我拨打以下行:
sendTextMessage(recipientID, "1");
sendTextMessage(recipientID, "2");
sendTextMessage(recipientID, "3");
sendTextMessage(recipientID, "4");
sendTextMessage(recipientID, "5");
我希望首先发送消息“ 1”。 然后,“ 2”,“ 3”等等。 (而不是随机发送,这是这里的问题。)
以下是相关的助手功能。
function sendTextMessage(recipientId, messageText) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: messageText
}
};
callSendAPI(messageData);
}
这是callSendAPI函数。
function callSendAPI(messageData) {
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: PAGE_ACCESS_TOKEN },
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
if (messageId) {
console.log("Successfully sent message with id %s to recipient %s",
messageId, recipientId);
} else {
console.log("Successfully called Send API for recipient %s",
recipientId);
}
} else {
console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
}
});
}
我被困了一段时间。 任何帮助将不胜感激。
我试过了,但是没有用。 =(
sendTextMessage(recipientID, "1")
.then(sendTextMessage(recipientID, "2"))
.then(sendTextMessage(recipientID, "3"));
在您的示例中,没有承诺,这就是为什么它不起作用的原因。 request
软件包不保证兼容,但是您可以安装request-promise
,这是bluebird
request
包装。
假设您正在使用request-promise
您的示例将如下所示:
function callSendAPI(messageData) {
// return the promise so you can use the promise where you call the function
return request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: PAGE_ACCESS_TOKEN },
method: 'POST',
json: messageData
}).then( function(body) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
if (messageId) {
console.log("Successfully sent message with id %s to recipient %s",
messageId, recipientId);
} else {
console.log("Successfully called Send API for recipient %s",
recipientId);
}
}).catch(function(error) {
console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
});
}
而对于其他功能:
function sendTextMessage(recipientId, messageText) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: messageText
}
};
return callSendAPI(messageData);
}
注意,您必须返回promise,否则调用该函数的行将无法使用promise。
为此,请使您的两个辅助函数返回promise。 因此,在callSendAPI
创建并返回一个,并且sendTextMessage
应该只返回从callSendAPI
获得的相同承诺。 最后,请确保将函数传递给您的then
调用,而不是执行一个函数。 您可以使用.bind()
从现有的函数创建一个新函数,并指定调用时应传递的参数。
function callSendAPI(messageData) {
return new Promise(function (resolve, reject) { // ***
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: PAGE_ACCESS_TOKEN },
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
if (messageId) {
console.log("Successfully sent message with id %s to recipient %s",
messageId, recipientId);
} else {
console.log("Successfully called Send API for recipient %s",
recipientId);
}
resolve(body); // ***
} else {
console.error("Failed calling Send API", response.statusCode,
response.statusMessage, body.error);
reject(body.error); // ***
}
});
});
}
function sendTextMessage(recipientId, messageText) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: messageText
}
};
return callSendAPI(messageData); // *** returns promise
}
sendTextMessage(recipientID, "1")
.then(sendTextMessage.bind(null, recipientID, "2")) // *** pass a function reference
.then(sendTextMessage.bind(null, recipientID, "3"))
.catch(function (body) {
console.log('aborted');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.