Loop through asynchronous request

后端 未结 3 1594
我在风中等你
我在风中等你 2020-11-28 17:08

so I have the following code to loop through a Object:

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


        
相关标签:
3条回答
  • 2020-11-28 17:11

    You can use async module which will be really helpful to you for making requests one by one. Below is a sample code from async official docs which is fairly intuitive to understand.

      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)
      })
    
    }
    
    0 讨论(0)
  • 2020-11-28 17:32

    One of the ways I've solved this in the past is to use an interval timer, sort of like this:

    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')
            }
        })
    }
    
    0 讨论(0)
  • 2020-11-28 17:38

    I ended up following @Matt Diamond advice and do a recursive function that looks like this:

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

    Thanks everyone who gave some help, greatly appreciated.

    0 讨论(0)
提交回复
热议问题