Yes, I know there are plenty of libraries to get this done (Q, step, async), but only for learning I'm wondering if the following code is ok and usable or have major drawbacks and it is only usable at a small programs.
Usage:
var g = new Manager();
g.then (
function() {
console.log("hello");
setTimeout( function () { g.done("hello done") }, 2000 );
}
);
g.start();
g.then (
function() {
console.log("two");
g.done ("two done");
}
);
g.then (
function(resp) {
console.log("I got: " + resp);
setTimeout( function () { g.done("happy ending") }, 2000 );
}
);
g.then (
function(resp) {
console.log("last msg: " + resp);
g.done();
}
);
Class:
//...............................
// class Manager
//...............................
function Manager () {
this.functionList = [];
this.allDone = false;
} // ()
//...............................
//...............................
Manager.prototype.then = function (fun) {
this.functionList.push (fun);
if (this.allDone) {
this.allDone = false;
this.done(this.previousResult);
console.log ( "then(): continuing");
}
} // ()
//...............................
//...............................
Manager.prototype.done = function (prevRes) {
this.previousResult = prevRes;
if ( this.where <= this.functionList.length-2 ) {
this.where++;
this.functionList [this.where](this.previousResult);
} else {
this.allDone = true;
console.log ( "done(): stopping because allDone: where=" + this.where + " and length = " + this.functionList.length);
}
} // ()
//...............................
//...............................
Manager.prototype.start = function () {
this.allDone = false;
this.where=-1;
this.done();
} // ()