For fun, I made a function queue in JavaScript. I named it tinyq. You can add functions to it, and each function is passed the next function in the queue. You can also pass parameters to the functions, and they can return values.
function q(){ var Q = [], t = this; this.length = 0; this.add = function (f){ Q.push(f); this.length++; }; this.run = function(){ var n = Q.shift(); this.length = Q.length; if(typeof n === 'function'){ var a = Array.prototype.slice; return n.apply(window,[function(){ return t.run.apply(t, a.call(arguments)); }].concat(a.call(arguments))); } }; }
Here's a simple example of its use.
var Q = new q; Q.add(function(next, A, B){ return A + ": "+ next(B); }); Q.add(function(next, C){ return C + "123"; }); console.log(Q.length); // 2 console.log(Q.run("Test", "ABC")); // "Test: ABC123"
So, what do you think of my function queue? Any improvements, or anything I'm doing wrong?
P.S. I was also trying to make this small, any suggestions on making it smaller?
Queue
implementation a few days ago when answering an SO question. Here is my CR OP: codereview.stackexchange.com/q/28380/3163 ^_^ Have fun with it!\$\endgroup\$