I am attempting to implement an asynchronous method queue in Javascript as seen in this blog post
Here's what I have so far:
function Queue() {
this._methods = [];
this._response = null;
this._flushed = false;
}
(function(Q){
Q.add = function (fn) {
if (this._flushed) fn(this._response);
else this._methods.push(fn);
}
Q.flush = function (response) {
if (this._flushed) return;
this._response = response;
while (this._methods[0]) {
this._methods.shift()(response);
}
this._flushed = true;
}
})(Queue.prototype);
I can't seem to get it to work as advertised, although the code looks correct to me. When I call the flush function I get this._methods is undefined
on the line while (this._methods[0]) {
.