-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
asyncGenerators.js
61 lines (55 loc) · 1.58 KB
/
asyncGenerators.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
var async = require('../../lib');
const {expect} = require('chai');
const assert = require('assert');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
module.exports = function () {
async function asyncIdentity(val) {
var res = await Promise.resolve(val);
return res;
}
async function * range (num) {
for(let i = 0; i < num; i++) {
await delay(1)
yield i
}
}
it('should handle async generators in each', (done) => {
const calls = []
async.each(range(5),
async (val) => {
calls.push(val)
await delay(5)
}, (err) => {
if (err) throw err
expect(calls).to.eql([0, 1, 2, 3, 4])
done()
}
)
});
it('should handle async generators in eachLimit', (done) => {
const calls = []
async.eachLimit(range(5), 2,
async (val) => {
calls.push(val)
await delay(5)
}, (err) => {
if (err) throw err
expect(calls).to.eql([0, 1, 2, 3, 4])
done()
}
)
});
it('should handle async generators in eachSeries', (done) => {
const calls = []
async.eachSeries(range(5),
async (val) => {
calls.push(val)
await delay(5)
}, (err) => {
if (err) throw err
expect(calls).to.eql([0, 1, 2, 3, 4])
done()
}
)
});
}