如何使用Node.js中的Promise对异步调用进行排序?

时间:2017-02-19 21:18:42

标签: javascript node.js promise facebook-messenger

我是Javascript的新手,无法理解如何让我的功能相继运行。我想用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"));

2 个答案:

答案 0 :(得分:2)

在您的示例中,没有承诺,这就是它无法正常工作的原因。 request包不承诺兼容,但您可以使用request-promise安装request bluebird的包装。

假设您正在使用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,否则调用该函数的行无法使用它。

答案 1 :(得分:2)

为了使这项工作,让你的两个辅助函数返回promises。因此,在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');
});
相关问题