summaryrefslogtreecommitdiff
path: root/mocha_test/asyncify.js
blob: b171e86a1de605c4d7b06b6bd624783ce96e4d75 (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');
var assert = require('assert');
var expect = require('chai').expect;
var isBrowser = require('./support/is_browser');

describe('asyncify', function(done){

    it('asyncify', function(done) {
        var parse = async.asyncify(JSON.parse);
        parse("{\"a\":1}", function (err, result) {
            assert(!err);
            assert(result.a === 1);
            done();
        });
    });

    it('asyncify null', function(done) {
        var parse = async.asyncify(function() {
            return null;
        });
        parse("{\"a\":1}", function (err, result) {
            assert(!err);
            assert(result === null);
            done();
        });
    });

    it('variable numbers of arguments', function(done) {
         async.asyncify(function (x, y, z) {
             return arguments;
         })(1, 2, 3, function (err, result) {
             expect(result.length).to.equal(3);
             expect(result[0]).to.equal(1);
             expect(result[1]).to.equal(2);
             expect(result[2]).to.equal(3);
             done();
         });
     });

    it('catch errors', function(done) {
        async.asyncify(function () {
            throw new Error("foo");
        })(function (err) {
            assert(err);
            assert(err.message === "foo");
            done();
        });
    });

    it('dont catch errors in the callback', function(done) {
        try {
            async.asyncify(function () {})(function (err) {
                if (err) {
                    return done(new Error("should not get an error here"));
                }
                throw new Error("callback error");
            });
        } catch (e) {
            assert(e.message === "callback error");
            done();
        }
    });

    describe('promisified', function() {
        if (isBrowser()) {
            // node only tests
            return;
        }

        var names = [
            'native-promise-only',
            'bluebird',
            'es6-promise',
            'rsvp'
        ];

        names.forEach(function(name) {
            describe(name, function() {

                var Promise = require(name);
                if (typeof Promise.Promise === 'function') {
                    Promise = Promise.Promise;
                }

                it('resolve', function(done) {
                    var promisified = function(argument) {
                        return new Promise(function (resolve) {
                            setTimeout(function () {
                                resolve(argument + " resolved");
                            }, 15);
                        });
                    };
                    async.asyncify(promisified)("argument", function (err, value) {
                        if (err) {
                            return done(new Error("should not get an error here"));
                        }
                        assert(value === "argument resolved");
                        done();
                    });
                });

                it('reject', function(done) {
                    var promisified = function(argument) {
                        return new Promise(function (resolve, reject) {
                            reject(argument + " rejected");
                        });
                    };
                    async.asyncify(promisified)("argument", function (err) {
                        assert(err);
                        assert(err.message === "argument rejected");
                        done();
                    });
                });
            });
        });
    });
});