Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

javascript - Loop through asynchronous request

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

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

For each iteration I want to do a request (send a facebook message), only after that request has finished, I want to go through the next iteraction, this is because without any callbacks, the messages won't be sent in the right succession.

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

I've had this problem before and not been able to solve it, how can I, somehow do this?

If you have any questions, please ask. Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...