Skip to content

Commit

Permalink
Added ability to pass results from one callback to another
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Skvira committed Jan 7, 2011
1 parent 09621f5 commit ad16382
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
41 changes: 30 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ function defaultErrorHandler(err) {
function runChain(chain, index) {
var chainIndex = index || 0,
currentElem,
currArgs = [],
argIndex = 0;
argIndex = 0,
currentArgs = [];

if (!chain || chain.length === 0) {
return;
Expand All @@ -24,7 +24,10 @@ function runChain(chain, index) {
function checkErrorCallback(err) {

var handler,
msg;
msg,
args,
resultArgs,
i = 0;

if (err) {
// If error handler callback is provided, call it supplying 'err' object,
Expand All @@ -35,19 +38,35 @@ function runChain(chain, index) {
}

chainIndex += 1;
runChain(chain, chainIndex);
}
debugger;
args = [chain, chainIndex];

// Add pre-defined arguments
if (currentElem.args) {
for (; argIndex < currentElem.args.length; argIndex++) {
currArgs.push(currentElem.args[argIndex]);
// If passResultToNextStep was specified, need to pass result of previous callback
// to next function in chain, skipping the 'err' object
if (currentElem.passResultToNextStep) {
resultArgs = Array.prototype.splice.apply(arguments, [1, arguments.length-1]);
for (; i < resultArgs.length; i++) {
args.push(resultArgs[i]);
}
}

runChain.apply(null, args);
}

currArgs.push(checkErrorCallback);
if (arguments.length > 2) {
// Looks like we were supplied agruments from previous call.
// Let's take away 'chain' and 'chainIndex' arguments and pass it on further
// down the call chain
currentArgs = Array.prototype.splice.apply(arguments, [2, arguments.length-2]);
} else {
// No arguments from prevous call, just use pre-defined arguments, if any
if (currentElem.args) {
currentArgs = currentElem.args.slice();
}
}

currentElem.target.apply(this, currArgs);
currentArgs.push(checkErrorCallback);
currentElem.target.apply(this, currentArgs);
}


Expand Down
31 changes: 30 additions & 1 deletion tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ chain = [
target: function(a, b, c, callback) {
test_args.push([a, b, c]);
test_stack.push(3);
callback(null);
callback(null, a, b, c);
},
args: [7, 8, 9]
}
Expand All @@ -163,3 +163,32 @@ assertArraysEqual(chain[0].args, test_args[0]);
assertArraysEqual(chain[1].args, test_args[1]);
assertArraysEqual(test_stack, [1, 2]);
assertEquals(ERROR_MESSAGE, test_error);


//
// 5. Test passing of callback results to the next function inthe chain
//
test_stack = [];
test_args = [];

chain = [
{
target: function(callback) {
test_stack.push(1);
callback(null, 1, 2, 3);
},
passResultToNextStep: true
},
{
target: function(a, b, c) {
test_stack.push(2);
test_args.push([a, b, c]);
//callback(null);
}
}
];

c.runChain(chain);

assertArraysEqual(test_stack, [1, 2]);
assertArraysEqual(test_args[0], [1, 2, 3]);

0 comments on commit ad16382

Please sign in to comment.