Skip to content

Commit

Permalink
All methods now return a promise
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaHegde committed Mar 11, 2015
1 parent c4ad862 commit 321a308
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 7 deletions.
21 changes: 15 additions & 6 deletions lib/promise-tracker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var Promise = require("promise");

function PromiseTracker() {
this.que = [];
this.runningAsyncCount = 0;
Expand All @@ -19,6 +21,7 @@ PromiseTracker.prototype.invokeNext = function() {
promise.then(function() {
tracker.runningNonBlocking -= process.type;
tracker.runningAsyncCount--;
process.resolve();
tracker.invokeNext();
}, function(err) {
for(var i = 0; i < tracker.que.length; i++) {
Expand All @@ -35,30 +38,36 @@ PromiseTracker.prototype.addAsync = function(type, args) {
var
_args = Array.prototype.slice.call(args),
fn = _args.shift(),
context = _args.pop();
this.que.push({
context = _args.pop(),
queObj = {
fn : fn,
context : context,
args : _args,
type : type,
},
promise = new Promise(function(resolve, reject) {
queObj.resolve = resolve;
queObj.reject = reject;
});
this.que.push(queObj);
this.invokeNext();
return promise;
};

PromiseTracker.prototype.addAsyncBlocking = function() {
this.addAsync(0, arguments);
return this.addAsync(0, arguments);
};

PromiseTracker.prototype.addAsyncNonBlocking = function() {
this.addAsync(1, arguments);
return this.addAsync(1, arguments);
};

PromiseTracker.prototype.wait = function() {
this.addAsync(0, [function() {}, this]);
return this.addAsync(0, [function() {}, this]);
};

PromiseTracker.prototype.andThen = function() {
this.addAsync(0, arguments);
return this.addAsync(0, arguments);
};

module.exports = PromiseTracker;
2 changes: 1 addition & 1 deletion tests/sanity-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tests = [{
}],
functions = ["addAsyncBlocking", "addAsyncNonBlocking"];

describe("Sanity Test", function() {
describe("Sanity Tests", function() {
for(var i = 0; i < tests.length; i++) {
(function() {
var
Expand Down
78 changes: 78 additions & 0 deletions tests/then-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var
assert = require("assert"),
Promise = require("../index").Promise,
PromiseTracker = require("../index").PromiseTracker,

tests = [{
promises : [0, 0, 0, 0, 0],
timeFun : function(idx) {
return 100;
},
order : [0, 1, 2, 3, 4],
title : "All blocking promises",
}, {
promises : [1, 1, 1, 1, 1],
timeFun : function(idx) {
return 150 - idx * 20;
},
order : [4, 3, 2, 1, 0],
title : "All non blocking promises",
}, {
promises : [0, 1, 1, 1, 0],
timeFun : function(idx) {
return 150 - idx * 20;
},
order : [0, 3, 2, 1, 4],
title : "Mixed : Starting and ending with blocking promises",
}, {
promises : [1, 1, 1, 1, 0],
timeFun : function(idx) {
return 150 - idx * 20;
},
order : [3, 2, 1, 0, 4],
title : "Mixed : Starting with non blocking promises",
}, {
promises : [0, 1, 1, 1, 1],
timeFun : function(idx) {
return 150 - idx * 20;
},
order : [0, 4, 3, 2, 1],
title : "Mixed : Ending with non blocking promises",
}],
functions = ["addAsyncBlocking", "addAsyncNonBlocking"];

describe("Then Tests", function() {
for(var i = 0; i < tests.length; i++) {
(function() {
var
test = tests[i];
it(test.title, function(done) {
var
tracker = new PromiseTracker(),
order = [],
checkOrder = function() {
if(order.length === test.promises.length) {
assert.deepEqual(order, test.order);
done();
}
};

for(var j = 0; j < test.promises.length; j++) {
(function() {
var _j = j;
tracker[functions[test.promises[j]]](function (pi) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve();
}, test.timeFun(pi));
});
}, j, this).then(function() {
order.push(_j);
checkOrder();
});
})();
}
});
})();
}
});

0 comments on commit 321a308

Please sign in to comment.