Commit a32fa7e1 authored by Johannes Bill's avatar Johannes Bill

added Mocha Test

parent 9b2e6653
function SimplQueue(finisher) {
this._finisher = finisher;
this._queue = [];
this._started = false;
}
SimplQueue.prototype.push = function (cb) {
this._queue.push(cb);
if (this._finisher && !this._started) {
this._started = true;
var next = this._queue.shift();
next(this._handler.bind(this));
}
};
SimplQueue.prototype.start = function () {
if (this._started) return;
this._started = true;
var next = this._queue.shift();
if (next)
next(this._handler.bind(this));
else this._finisher();
};
SimplQueue.prototype._handler = function (err) {
if (err) return this._finisher(err);
var next = this._queue.shift();
if (next) {
next(this._handler.bind(this));
}
else this._finisher(null);
};
module.exports = SimplQueue;
function doStuff() {
var queue = new SimplQueue(function (err) {
console.log(err);
console.log("finished");
});
for (var i = 0; i < 100; i++) {
queue.push(function (cb) {
setTimeout(function () {
if(Math.random() > 0.9)
return cb(new Error("muh"));
console.log(+new Date());
cb(null);
}, 50)
})
}
}
doStuff();
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment