0

Want to make a lot of asynchronous requests. The number of requests from 10 to 1000, should write down the answer in array (order is not important, no dependency)

   while (i<5) {
        api.get("get", offset: i, function(result) {
            datag = datag.concat(data.id);
        })
    i++;
    }

Is it right way? Is there a particular? the synchronous javascript used with asynchronous requests and i afraid of unexpected situations

2
  • What is api.get? Why so many request? Is there not a way you could do it in less requests? Commented Mar 14, 2014 at 16:00
  • This is an example query to api. Practical task - collecting public page posts from social network. Commented Mar 14, 2014 at 16:05

1 Answer 1

1

No, this is not the correct way. You are making the requests in parallel. It may work for 5 request, but for thousands, some will surely timeout.

You need to make each request trigger the next request in its success timeout:

var i = 0;

function success (result) {
   datag = datag.concat(result.id);

   // launch the next request
   if (++i < 5) {
     api.get("get", offset: i, success)
   }
}

// launch the first request
api.get("get", offset: i, success);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.