forked from tim-smart/async-array
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
84 lines (75 loc) · 1.73 KB
/
test.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
84
var AsyncArray = require('./')
var items = new AsyncArray([1, 2, 'three', 4])
items
.map(function (item, i, next) {
setTimeout(next, 1000, null, 'did ' + i)
})
.done(function (error, results) {
console.log('map', results)
})
.exec()
items
.mapSerial(function (item, i, next) {
setTimeout(next, 1000, null, 'did ' + i)
})
.done(function (error, results) {
console.log('mapSerial', results, results.length)
})
.exec()
items
.filter(function (item, i, next) {
setTimeout(function () {
if ('string' === typeof item) {
return next()
}
next(null, true)
}, 1000)
})
.exec(function (error, results) {
console.log('filter', results)
})
items
.forEachSerial(function (item, i, next) {
setTimeout(function () {
console.log('forEachSerial', i, item)
next()
}, 1000)
})
.exec()
items
.map(function (item, i, next) {
if ('string' === typeof item) {
return next(new Error('stupid'))
}
next(null, item)
})
.exec(function (error) {
console.log('error', error)
})
items
.filter(function (item, i, next) {
if ('string' === typeof item) {
return next()
}
next(null, true)
})
.done(function (error, results) {
console.log('chain filter', results)
})
.map(function (item, i, next) {
next(null, 'item ' + i + ': ' + item)
})
.exec(function (error, results) {
console.log('chain map', results)
results.array()
console.log('normal array', 'undefined' === typeof results.array)
})
var op = new AsyncArray.Operation;
op.forEachSerial(function foreach (item, i, next) {
console.log('forEachSaved', i)
next()
})
op = op.save()
op([1, 2, 3], function (err) {
console.log('forEachSaved', 'done')
})