summaryrefslogtreecommitdiff
path: root/test/applyEach.js
blob: a5fce954ebfca829d91423ae519e56205789ce24 (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
var async = require('../lib');
var {expect} = require('chai');
var assert = require('assert');

describe('applyEach', () => {

    it('applyEach', (done) => {
        var call_order = [];
        var one = function (val, cb) {
            expect(val).to.equal(5);
            setTimeout(() => {
                call_order.push('one');
                cb(null, 1);
            }, 12);
        };
        var two = function (val, cb) {
            expect(val).to.equal(5);
            setTimeout(() => {
                call_order.push('two');
                cb(null, 2);
            }, 2);
        };
        var three = function (val, cb) {
            expect(val).to.equal(5);
            setTimeout(() => {
                call_order.push('three');
                cb(null, 3);
            }, 18);
        };
        async.applyEach([one, two, three], 5)((err, results) => {
            assert(err === null, err + " passed instead of 'null'");
            expect(call_order).to.eql(['two', 'one', 'three']);
            expect(results).to.eql([1, 2, 3]);
            done();
        });
    });

    it('applyEach canceled', (done) => {
        var call_order = [];
        function one(_, cb) {
            call_order.push('one');
            cb(null, 1);
        }

        function two(_, cb) {
            call_order.push('two');
            cb(false);
        }

        function three(/*, cb */) {
            throw new Error('third task - should not get here');
        }

        async.applyEach({one, two, three}, 5)(() => {
            throw new Error('final callback - should not get here');
        });

        setTimeout(() => {
            expect(call_order).to.eql(['one', 'two']);
            done();
        }, 25);
    });

    it('applyEachSeries', (done) => {
        var call_order = [];
        function one(val, cb) {
            expect(val).to.equal(5);
            setTimeout(() => {
                call_order.push('one');
                cb(null, 1);
            }, 10);
        }
        function two(val, cb) {
            expect(val).to.equal(5);
            setTimeout(() => {
                call_order.push('two');
                cb(null, 2);
            }, 5);
        }
        function three(val, cb) {
            expect(val).to.equal(5);
            setTimeout(() => {
                call_order.push('three');
                cb(null, 3);
            }, 15);
        }
        async.applyEachSeries([one, two, three], 5)((err, results) => {
            assert(err === null, err + " passed instead of 'null'");
            expect(call_order).to.eql(['one', 'two', 'three']);
            expect(results).to.eql([1, 2, 3]);
            done();
        });
    });

    it('applyEachSeries canceled', (done) => {
        var call_order = [];
        function one(_, cb) {
            async.setImmediate(() => {
                call_order.push('one');
                cb(null, 1);
            });
        }
        function two(_, cb) {
            async.setImmediate(() => {
                call_order.push('two');
                cb(false);
            });
        }
        function three(/*, cb */) {
            throw new Error('third task - should not get here');
        }
        async.applyEachSeries([one, two, three], 5)(() => {
            throw new Error('final callback - should not get here');
        });

        setTimeout(() => {
            expect(call_order).to.eql(['one', 'two']);
            done();
        }, 25);
    });
});