Image

Asynchronous Looping in JavaScript

JavaScript in a web browser window is an event-driven, but single-threaded environment. When a JavaScript code unit starts executing, it has the undivided attention of the browser window until it’s finished executing. When such an atomic action requires so much time that the user perceives this unresponsiveness, the popular solution is to divide that action into smaller pieces and perform the pieces asynchronously, using setTimeout or setInterval.

This is simple when all the pieces are essentially the same: when you can just iterate through them and do them one after another. But when the task is a little more complex, with nested loops and conditional logic, the splitting up of that task becomes a lot more complex. I’ve written some sugar that factors out nearly all of the code required for managing the program’s asynchronicity, allowing your code to look more natural.

Complete Article:
http://www.tumuski.com/2008/07/asynchronous-looping/