Files: 7f5b9989dbc45ee625499416f98f9ce28de20509 / queue.js
522 bytesRaw
1 | |
2 | |
3 | module.exports = function (async) { |
4 | |
5 | var queue = [], working = false, prev = null |
6 | |
7 | function start () { |
8 | if(working) return |
9 | working = true |
10 | ;(function next (item) { |
11 | async(item.value, prev, function (err, result) { |
12 | prev = result |
13 | if(item.cb) item.cb(err, result) |
14 | if(queue.length) next(queue.shift(), result) |
15 | else |
16 | working = false |
17 | }) |
18 | })(queue.shift()) |
19 | } |
20 | |
21 | return function (value, cb) { |
22 | queue.push({value: value, cb: cb}) |
23 | start() |
24 | } |
25 | |
26 | } |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 |
Built with git-ssb-web