Sunday, March 23, 2014

ooph... getting owned by asynchronicity...

You thought I would have learned how asynchronous stuff works after claiming to be a javascript fan.

Well, I guess I haven't tried putting callbacks inside javascript for loops... **it doesn't work**!!


Here is a nice post on how to do it.
http://www.richardrodger.com/2011/04/21/node-js-how-to-write-a-for-loop-with-callbacks/#.Uy9m1uddXWN

What you need to do is

var array = [...];
repeater(i){
    if(i < array.length){
        asynch(array[i], function(){
            //do your stuff here...
            repeater(i++);
        }
    }
}
repeater(0);

Pretty tricky indeed!

No comments:

Post a Comment