javascript - Asynchronous looping -


good time of day! started learning javascript , i'm having difficulty asynchronous requests. have lot of requests server in short time possible.

for (i=0;i<10;i++;) {     method.call(get, data: "id", function(result) {         id = id.concat(data.id);     }) } 

i think way incorrect since request can not processed in time , has risk of not getting part of information.

if understand mean, there universal method used in such cases , there links articles details of working asynchrony , emerging issues explained? sorry long question.

if timing doesn't matter, want use concept called promises. http://www.promisejs.org/intro/ , http://blogs.msdn.com/b/ie/archive/2011/09/11/asynchronous-programming-in-javascript-with-promises.aspx intros. in pseudo code:

when(somethinghappens).then(dosomething) 

you can set 10 of execute , not block each other, presuming browser allows many connections.

if timing matters (b can't happen until completed) want use callbacks.

getstuff(thinga, function(){     // stuff thinga , then:     getstuff(thingb, function(){         // stuff thingb , then:         getstfuff(thingc, function            // , on        }     } } 

of course, callbacks don't need nested hell of anonymous function calls. can name each , next call within function.

getstuff(thinga, docallbackb);  function docallbackb (stufffromthinga) {     // stuff stufffromthinga ,     getstuff(thingb, docallbackc); } 

more on callbacks:

  1. https://developer.mozilla.org/en-us/docs/web/api/xmlhttprequest/synchronous_and_asynchronous_requests
  2. http://recurial.com/programming/understanding-callback-functions-in-javascript/
  3. http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -