Skip to content

Instantly share code, notes, and snippets.

@5long
Created October 13, 2010 11:48
Show Gist options
  • Save 5long/623874 to your computer and use it in GitHub Desktop.
Save 5long/623874 to your computer and use it in GitHub Desktop.
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
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