Skip to content

Commit

Permalink
return value for chain with no delay
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradhan V committed Nov 1, 2017
1 parent a459cb6 commit a82a526
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
7 changes: 4 additions & 3 deletions chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Chain.prototype._callFunctions = function (delay, functions, ...args) {
if (this._callback) {
this._callback(previousResult);
}
return;
return previousResult;
}
let fnReturnValue = null;
try {
Expand All @@ -52,7 +52,7 @@ Chain.prototype._callFunctions = function (delay, functions, ...args) {
if (this._callback) {
this._callback(err);
}
return;
return previousResult;
}
// remove the invoked function from the array
const nextFunctions = functions.slice(1);
Expand Down Expand Up @@ -87,8 +87,9 @@ Chain.prototype._callFunctions = function (delay, functions, ...args) {
}, delay);
} else {
// TODO: fix stackoverflow
that._callFunctions(...callargs);
fnReturnValue = that._callFunctions(...callargs);
}
return fnReturnValue;
};
//
const create = function (functionArray, callback, ...args) {
Expand Down
22 changes: 21 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ describe('#tests, args while start and create, with delay', () => {
});

describe('#chain', () => {
it('no delay, return from function', (done) => {
const val = chain.create([() => {}, () => {}, () => 100]).startCalls();
expect(val).to.equal(100);
done();
});
it('no delay, return from function 2', (done) => {
const val = chain.create([() => 1, () => {}, () => 101]).startCalls();
expect(val).to.equal(101);
done();
});
it('no delay, return from function 3', (done) => {
const val = chain.create([addNextCharWrap, addNextCharWrap, addNextCharWrap]).startCalls('j');
expect(val).to.equal('jklm');
done();
});
it('use set functions', (done) => {
const fchain = chain.create();
fchain.setDelay(delay);
Expand Down Expand Up @@ -286,13 +301,18 @@ describe('#chain', () => {
});
// repeat tests
describe('#repeat()', () => {
it('repeat function call', (done) => {
it('repeat function call, check final value in callback', (done) => {
const fc = chain.createRepeatFunctionChain(addOneWrap, (error, result) => result !== 100, (finalVal) => {
expect(finalVal).to.equal(100);
done();
}, 0);
fc.startCalls();
});
it.skip('repeat function call, check final returned value.', () => {
const fc = chain.createRepeatFunctionChain(addOneWrap, (error, result) => result !== 100);
const val = fc.startCalls(0);
expect(val).to.equal(100);
});
it('repeat function call, no continue callback function', (done) => {
const fc = chain.createRepeatFunctionChain(addOneWrap, undefined, (finalVal) => {
expect(finalVal).to.equal(1);
Expand Down

0 comments on commit a82a526

Please sign in to comment.