1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
module.exports = chain
// steps, cb
// Each step is either:
// [fn, arg, arg, arg] or
// [obj, "method", arg, arg, arg]
// The eventual cb passed to chain is resolved with an array of
// all the results, and/or an error if one was returned.
function chain () /* step, step, ..., cb */ {
var steps
, cb
if (arguments.length === 1 && Array.isArray(arguments[0])) {
steps = arguments[0]
} else {
steps = Array.prototype.slice.call(arguments, 0)
}
cb = steps.pop()
if (typeof(cb) !== "function") {
throw new Error("Invalid callback: "+typeof(cb))
}
;(function S (step, results) {
if (!step) return cb(null, results)
var obj
, fn
function callback (er, ok) {
if (er) return cb(er, results)
results.push(ok)
S(steps.shift(), results)
}
if (Array.isArray(step)) {
while (step[0] && !fn) switch (typeof step[0]) {
case "object" : obj = step.shift(); break
case "function" : fn = step.shift(); break
default : fn = obj[step.shift()]; break
}
step.push(callback)
} else fn = step
if (typeof fn !== "function") throw new Error(
"Non-function in chain() "+typeof(fn))
try {
if (Array.isArray(step)) fn.apply(obj, step)
else fn(callback)
} catch (er) {
cb(er, results)
}
})(steps.shift(), [])
}
|