shawndumas

Async Method Queue Chaining (Dustin Diaz)

See Diaz’s article.

Head over to remy sharp’s JS Bin and play.

 1 var Queue = function () {
 2   if (!(this instanceof arguments.callee)) {
 3       return new arguments.callee();
 4   }
 5   this._methods = [];
 6   this._response = null;
 7   this._flushed = false;
 8 }
 9 
10 Queue.prototype = {
11   add: function(fn) {
12     (this._flushed) ?
13       fn(this._response) :
14       this._methods.push(fn);
15   },
16   flush: function(resp) {
17     (this._flushed) && (return);
18     this._response = resp;
19     this._flushed = true;
20     while (this._methods[0]) {
21       this._methods.shift()(resp);
22     }
23   }
24 };
11
To Posterous, Love Metalab