summaryrefslogtreecommitdiff
path: root/test/series.js
blob: 48e9379d453e8e62da6137e79dd8e27c5dffd0c8 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var async = require('../lib');
var {expect} = require('chai');
var assert = require('assert');
var getFunctionsObject = require('./support/get_function_object');

describe('series', () => {
    it('series', (done) => {
        var call_order = [];
        async.series([
            function(callback){
                setTimeout(() => {
                    call_order.push(1);
                    callback(null, 1);
                }, 25);
            },
            function(callback){
                setTimeout(() => {
                    call_order.push(2);
                    callback(null, 2);
                }, 50);
            },
            function(callback){
                setTimeout(() => {
                    call_order.push(3);
                    callback(null, 3,3);
                }, 15);
            }
        ],
        (err, results) => {
            assert(err === null, err + " passed instead of 'null'");
            expect(results).to.eql([1,2,[3,3]]);
            expect(call_order).to.eql([1,2,3]);
            done();
        });
    });

    it('with reflect', (done) => {
        var call_order = [];
        async.series([
            async.reflect((callback) => {
                setTimeout(() => {
                    call_order.push(1);
                    callback(null, 1);
                }, 25);
            }),
            async.reflect((callback) => {
                setTimeout(() => {
                    call_order.push(2);
                    callback(null, 2);
                }, 50);
            }),
            async.reflect((callback) => {
                setTimeout(() => {
                    call_order.push(3);
                    callback(null, 3,3);
                }, 15);
            })
        ],
        (err, results) => {
            assert(err === null, err + " passed instead of 'null'");
            expect(results).to.eql([
                { value: 1 },
                { value: 2 },
                { value: [3,3] }
            ]);
            expect(call_order).to.eql([1,2,3]);
            done();
        });
    });

    it('empty array', (done) => {
        async.series([], (err, results) => {
            expect(err).to.equal(null);
            expect(results).to.eql([]);
            done();
        });
    });

    it('error', (done) => {
        async.series([
            function(callback){
                callback('error', 1);
            },
            function(callback){
                assert(false, 'should not be called');
                callback('error2', 2);
            }
        ],
        (err) => {
            expect(err).to.equal('error');
        });
        setTimeout(done, 100);
    });

    it('canceled', (done) => {
        async.series([
            function(callback) {
                callback(false, 1);
            },
            function(callback) {
                assert(false, 'second function should not be called');
                callback('error2', 2);
            }
        ], () => {
            assert(false, 'final callback should not be called');
        });

        setTimeout(done, 25);
    });

    it('error with reflect', (done) => {
        async.series([
            async.reflect((callback) => {
                callback('error', 1);
            }),
            async.reflect((callback) => {
                callback('error2', 2);
            }),
            async.reflect((callback) => {
                callback(null, 1);
            }),
            async.reflect((callback) => {
                callback('error3');
            })
        ],
        (err, results) => {
            assert(err === null, err + " passed instead of 'null'");
            expect(results).to.eql([
                { error: 'error', value: 1 },
                { error: 'error2', value: 2 },
                { value: 1 },
                { error: 'error3' },
            ]);
            done();
        });
    });

    it('no callback', (done) => {
        async.series([
            function(callback){callback();},
            function(callback){callback(); done();},
        ]);
    });

    it('object', (done) => {
        var call_order = [];
        async.series(getFunctionsObject(call_order), (err, results) => {
            expect(err).to.equal(null);
            expect(results).to.eql({
                one: 1,
                two: 2,
                three: [3,3]
            });
            expect(call_order).to.eql([1,2,3]);
            done();
        });
    });

    it('call in another context @nycinvalid @nodeonly', (done) => {
        var vm = require('vm');
        var sandbox = {
            async,
            done
        };

        var fn = "(" + (function () {
            async.series([function (callback) {
                callback();
            }], (err) => {
                if (err) {
                    return done(err);
                }
                done();
            });
        }).toString() + "())";

        vm.runInNewContext(fn, sandbox);
    });

    // Issue 10 on github: https://github.com/caolan/async/issues#issue/10
    it('falsy return values', (done) => {
        function taskFalse(callback) {
            async.nextTick(() => {
                callback(null, false);
            });
        }
        function taskUndefined(callback) {
            async.nextTick(() => {
                callback(null, undefined);
            });
        }
        function taskEmpty(callback) {
            async.nextTick(() => {
                callback(null);
            });
        }
        function taskNull(callback) {
            async.nextTick(() => {
                callback(null, null);
            });
        }
        async.series(
            [taskFalse, taskUndefined, taskEmpty, taskNull],
            (err, results) => {
                expect(results.length).to.equal(4);
                assert.strictEqual(results[0], false);
                assert.strictEqual(results[1], undefined);
                assert.strictEqual(results[2], undefined);
                assert.strictEqual(results[3], null);
                done();
            }
        );
    });
});