-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
83 lines (65 loc) · 1.4 KB
/
example.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var Queue = require('better-queue');
var q = new Queue(function(input, cb) {
console.log('Loop', input);
cb(null, input * -1);
}, {
concurrent: 3
});
for (var i = 0; i < 10; i++) {
q.push(i, function(err, result) {
console.log('Done', result);
});
}
// add jobs using the familiar Array API
/*
q.push(
function(cb) {
results.push('four')
cb()
},
function(cb) {
results.push('five')
cb()
}
)
// jobs can accept a callback or return a promise
q.push(function() {
return new Promise(function(resolve, reject) {
results.push('one')
resolve()
})
})
q.unshift(function(cb) {
results.push('one')
cb()
})
q.splice(2, 0, function(cb) {
results.push('three')
cb()
})
// use the timeout feature to deal with jobs that
// take too long or forget to execute a callback
q.timeout = 100
q.on('timeout', function(next, job) {
console.log('job timed out:', job.toString().replace(/\n/g, ''))
next()
})
q.push(function(cb) {
setTimeout(function() {
console.log('slow job finished')
cb()
}, 200)
})
q.push(function(cb) {
console.log('forgot to execute callback')
})
// get notified when jobs complete
q.on('success', function(result, job) {
console.log('job finished processing:', job.toString().replace(/\n/g, ''))
})
// begin processing, get notified on end / failure
q.start(function(err) {
if (err) throw err
console.log('all done:', results)
})
*/