Created
October 13, 2010 11:48
-
-
Save 5long/623874 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var makeArray = Function.prototype.call.bind(Array.prototype.slice) | |
function chaining(actions, initialParams) { | |
var action = actions.shift() | |
if (!action) return | |
function innerCallback(err) { | |
initialParams.shift() | |
var newParams = makeArray(arguments) | |
, passingParams = newParams.concat(initialParams) | |
chaining(actions, passingParams) | |
} | |
action.apply(innerCallback, initialParams) | |
} | |
function chain() { | |
var actions = makeArray(arguments) | |
process.nextTick(function() { | |
chaining(actions, []) | |
}) | |
} | |
module.exports = chain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var chain = require('./chain') | |
, assert = require('assert') | |
, recording = {steps: 3} | |
chain( | |
function() { | |
recording.steps-- | |
setTimeout(this, 5, NaN, 1) | |
} | |
, function(err, one) { | |
recording.steps-- | |
assert.notStrictEqual(err, err, "It's NaN") | |
assert.equal(one, 1, "Gotcha") | |
recording.status = 0 | |
process.nextTick(this.bind(null, Error(), 2, "42")) | |
} | |
, function(err, two, answer, one) { | |
recording.steps-- | |
assert.ok(err instanceof Error) | |
assert.equal(answer, 42) | |
assert.equal(one, 1, "You again, huh?") | |
assert.equal(arguments.length, two * two) | |
} | |
) | |
setTimeout(function() { | |
process.exit(recording.steps) | |
}, 50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment