Async Javascript Library

For making code run...when you want it to.

Goal

to force asynchronous code (AJAX, setTimeout, custom event, etc.) to be run in a user-defined order. I.e., if we have:

     function A() { ...some code... }
     function B() { ...some code... }
     function C() { ...some code... }
     function D() { ...some code... }
     

If A() and B() can be initiated at any point in time, but must execute in the order A()->B()->C()->D(), then each function must be fired only AFTER the previous one is complete. That's where Async comes in.

     var q = new Async.Observer();
     
     /*
         Each function must tell the queue that it is completed:
     */
     
     function A() { 
         // ... same code as before ...
         
         q.notify('A-completed');
     }
     function B() { 
         // ... same code as before ...
         
         q.notify('B-completed');
     }
     function C() { 
         // ... same code as before ...
         
         q.notify('C-completed');
     }
     function D() { 
         // ... same code as before ...
     }
     
     /*
         Tell each function when it should run:
     */
     
     q.subscribe(A, 'start-running');
     q.subscribe(B, 'A-completed');
     q.subscribe(C, 'B-completed');
     q.subscribe(D, 'C-completed');
     
     /*
         Start the chain running:
     */
     
     q.notify('start-running');
     

Download

Download version 1.0 here.

Example

View source for the code for this example. Here we have a div that cannot be shown until it's populated with data from an ajax call.