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:
Comments
Post a Comment