summaryrefslogtreecommitdiff
path: root/test/es2017/asyncGenerators.js
blob: 0fe8ac6724c9ff59bbd3d9c8fc511560d5948b79 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var async = require('../../lib');
const {expect} = require('chai');

const delay = ms => new Promise(resolve => setTimeout(resolve, ms))

module.exports = function () {
    async function * range (num) {
        for(let i = 0; i < num; i++) {
            await delay(1)
            yield i
        }
    }

    function AsyncIterable (size) {
        // cant use method shorthand because babel doesnt parse it right
        async function * iterator () {
            let idx = 0
            while (idx < size) {
                yield idx
                await delay(1)
                idx++
            }
        }
        return {
            [Symbol.asyncIterator]: iterator
        }
    }

    this.retries(3);

    it('should handle async generators in each', (done) => {
        const calls = []
        async.each(range(5),
            async (val) => {
                calls.push(val)
                await delay(1)
            }, (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()
            }
        )
    });


    it('should handle async iterables in each', (done) => {
        const calls = []
        async.each(new AsyncIterable(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 iterables in each (errors)', (done) => {
        const calls = []
        async.each(new AsyncIterable(5),
            async (val) => {
                calls.push(val)
                if (val === 3) throw new Error('fail')
                await delay(5)
            }, (err) => {
                expect(err.message).to.equal('fail')
                expect(calls).to.eql([0, 1, 2, 3])
                done()
            }
        )
    })

    it('should handle async iterables in each (cancelled)', async () => {
        const calls = []
        async.each(new AsyncIterable(5),
            (val, cb) => {
                calls.push(val)
                if (val === 3) cb(false)
                cb()
            }, () => {
                throw new Error('should not get here')
            }
        )
        await delay(10)
        expect(calls).to.eql([0, 1, 2, 3])
    })
}