循环遍历异步请求

时间:2017-06-19 17:26:31

标签: javascript node.js loops express

所以我有以下代码来遍历一个Object:

for(var x in block){
    sendTextMessage(block[x].text, sender, function(callback){
        //increment for?
    })
}

对于每次迭代,我想做一个请求(发送一条facebook消息),只有在那个请求完成之后,我才想进行下一次迭代,这是因为没有任何回调,这些消息不会成为以正确的继承方式发送。

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')
        }
    })
}

我之前遇到过这个问题而无法解决问题,我怎么能这样做呢?

如果您有任何疑问,请询问。谢谢。

3 个答案:

答案 0 :(得分:2)

您可以使用async模块,这对您一个一个地提出请求非常有帮助。以下是来自异步官方文档的示例代码,这些代码非常直观易懂。

  function asyncForEach (arr, iterator, callback) {
    queue = arr.slice(0)
        // create a recursive iterator
    function next (err) {
      if (err) return callback(err)

            // if the queue is empty, call the callback with no error
      if (queue.length === 0) return callback(null)

            // call the callback with our task
            // we pass `next` here so the task can let us know when to move on to the next task
      iterator(queue.shift(), next)
    }

        // start the loop;
    next()
  }

function sampleAsync (param, done) {
// put a callback when function is done its work 
}

  asyncForEach(result, function (param, done) { // result is the array you pass as iterator
    sampleAsync(param, function (message) {
      console.log(message)
      done()
    })
  }, function () {
    console.log('callback')
    callback(SOME_RESULT)
  })

}

答案 1 :(得分:0)

过去我解决这个问题的方法之一就是使用间隔计时器,如下所示:

var isSending = false;
var sendMessages = setInterval(function() {
  if(!isSending) {
    isSending = true;
    sendTextMessage(block.pop().text, sender, function(){
      if(block.length) {
       isSending = false;
      } else {
        clearInterval(sendMessages);
        //Done
      }
    })
  }
})

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')
        }
    })
}

答案 2 :(得分:0)

我最后关注@Matt Diamond建议并执行一个如下所示的递归函数:

function buildFlow(block, sender){
    var i = 0;
    recursive()
    /*  for(var x in block){
        sendTextMessage(block[x], block[x].type, sender)
        console.log(x)
    }*/
    function recursive(){
        if (i<Object.keys(block).length){
            sendTextMessage(block[Object.keys(block)[i]], block[Object.keys(block)[i]].type, sender, function(){
                i++
                recursive()
            })

        }else{
            i = 0
        }
    }
}

感谢所有提供帮助的人,非常感谢。