-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathasyncify.js
202 lines (180 loc) · 6.65 KB
/
asyncify.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
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
var async = require('../lib');
var assert = require('assert');
var {expect} = require('chai');
describe('asyncify', () => {
it('asyncify', (done) => {
var parse = async.asyncify(JSON.parse);
parse("{\"a\":1}", (err, result) => {
assert(!err);
expect(result.a).to.equal(1);
done();
});
});
it('asyncify null', (done) => {
var parse = async.asyncify(() => {
return null;
});
parse("{\"a\":1}", (err, result) => {
assert(!err);
expect(result).to.equal(null);
done();
});
});
it('variable numbers of arguments', (done) => {
const fn = (...args/*x, y, z*/) => args
async.asyncify(fn)(1, 2, 3, (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', (done) => {
async.asyncify(() => {
throw new Error("foo");
})((err) => {
assert(err);
expect(err.message).to.equal("foo");
done();
});
});
it('dont catch errors in the callback', (done) => {
try {
async.asyncify(() => {})((err) => {
if (err) {
return done(new Error("should not get an error here"));
}
throw new Error("callback error");
});
} catch (err) {
expect(err.message).to.equal("callback error");
done();
}
});
it('propagates error with empty message without modification', (done) => {
let originalErr;
async.asyncify(async () => {
originalErr = new AggregateError([new Error('foo'), new Error('bar')]);
throw originalErr;
})((err) => {
assert(err);
assert.strictEqual(err, originalErr);
expect(err.errors).to.be.an('array');
expect(err.errors.length).to.equal(2);
expect(err.errors[0].message).to.equal('foo');
expect(err.errors[1].message).to.equal('bar');
done();
})
})
describe('promisified', () => {
function promisifiedTests(Promise) {
it('resolve', (done) => {
var promisified = function(argument) {
return new Promise(((resolve) => {
setTimeout(() => {
resolve(argument + " resolved");
}, 15);
}));
};
async.asyncify(promisified)("argument", (err, value) => {
if (err) {
return done(new Error("should not get an error here"));
}
expect(value).to.equal("argument resolved");
done();
});
});
it('reject', (done) => {
var promisified = function(argument) {
return new Promise(((resolve, reject) => {
reject(argument + " rejected");
}));
};
async.asyncify(promisified)("argument", (err) => {
assert(err);
expect(err.message).to.equal("argument rejected");
done();
});
});
it('reject without reason', (done) => {
var promisified = function() {
return new Promise(((resolve, reject) => {
reject();
}));
};
async.asyncify(promisified)("argument", (err) => {
assert(err);
expect(err.message).to.eql('');
done();
});
});
it('callback error @nodeonly', (done) => {
expectUncaughtException();
var promisified = function(argument) {
return new Promise(((resolve) => {
resolve(argument + " resolved");
}));
};
var call_count = 0;
async.asyncify(promisified)("argument", () => {
call_count++;
if (call_count === 1) {
throw new Error("error in callback");
}
});
setTimeout(() => {
expect(call_count).to.equal(1);
done();
}, 15);
});
it('dont catch errors in the callback @nodeonly', (done) => {
expectUncaughtException(checkErr);
var callbackError = new Error('thrown from callback');
function checkErr(err) {
expect(err).to.equal(callbackError);
done();
}
function callback() {
throw callbackError;
}
async.asyncify(() => {
return Promise.reject(new Error('rejection'));
})(callback);
});
}
describe('native-promise-only', function() {
var Promise = require('native-promise-only');
promisifiedTests.call(this, Promise);
});
describe('bluebird', function() {
var Promise = require('bluebird');
// Bluebird reports unhandled rejections to stderr. We handle it because we expect
// unhandled rejections:
Promise.onPossiblyUnhandledRejection(() => {});
promisifiedTests.call(this, Promise);
});
describe('es6-promise', function() {
var {Promise} = require('es6-promise');
promisifiedTests.call(this, Promise);
});
describe('rsvp', function() {
var {Promise} = require('rsvp');
promisifiedTests.call(this, Promise);
});
function expectUncaughtException(onError) {
// do a weird dance to catch the async thrown error before mocha
var listeners = process.listeners('uncaughtException');
process.removeAllListeners('uncaughtException');
process.once('uncaughtException', (err) => {
listeners.forEach((listener) => {
process.on('uncaughtException', listener);
});
// can't throw errors in a uncaughtException handler, defer
if (onError) {
setTimeout(onError, 0, err);
}
});
}
});
});