9fc26b417c168580e86e52fadb1df88602cc651b.svn-base
1.01 KB
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
module.exports = loop
// this weird little engine is to loop if the cb's keep getting
// called synchronously, since that's faster and makes shallower
// stack traces, but recurse if any of them don't fire this tick
function once (cb) {
var called = false
return function (er) {
if (called)
return
called = true
return cb.call(this, er)
}
}
function loop (self, arr, cb, onerr, i) {
if (!i)
i = 0
var running = false
while (i < arr.length && !running) {
var onceNext = once(next)
running = true
var sync = true
try {
var ret = arr[i].call(self, onceNext)
} catch (er) {
return onerr.call(self, er)
}
if (ret && typeof ret.then === 'function')
ret.then(onceNext.bind(self, null), onerr.bind(self))
i++
sync = false
}
function next (er) {
if (er)
return onerr.call(self, er)
else if (!sync)
return loop(self, arr, cb, onerr, i)
running = false
}
if (i >= arr.length && !running)
return cb.call(self)
}