-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathasync.js
More file actions
277 lines (231 loc) · 6.28 KB
/
Copy pathasync.js
File metadata and controls
277 lines (231 loc) · 6.28 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
'use strict';
const test = require('node:test');
const assert = require('node:assert');
const metautil = require('..');
const { toBool, timeout, delay } = metautil;
const { timeoutify, throttle, debounce } = metautil;
const { callbackify, asyncify, promisify } = metautil;
test('Async: toBool', async () => {
const success = await Promise.resolve('success').then(...toBool);
assert.strictEqual(success, true);
const rejected = await Promise.reject(new Error('Ups')).then(...toBool);
assert.strictEqual(rejected, false);
});
test('Async: Abortable timeout', async () => {
try {
await timeout(10);
assert.ifError(new Error('Should not be executed'));
} catch (err) {
assert.strictEqual(err.code, 'ETIMEOUT');
assert.strictEqual(err.message, 'Timeout of 10ms reached');
}
const ac = new AbortController();
setTimeout(() => {
ac.abort();
}, 10);
try {
await timeout(100, ac.signal);
assert.ifError(new Error('Should not be executed'));
} catch (err) {
assert.strictEqual(err.message, 'Timeout aborted');
}
});
test('Async: Abortable delay', async () => {
try {
const res = await delay(10);
assert.strictEqual(res, undefined);
} catch {
assert.ifError(new Error('Should not be executed'));
}
const ac = new AbortController();
setTimeout(() => {
ac.abort();
}, 10);
try {
await delay(100, ac.signal);
assert.ifError(new Error('Should not be executed'));
} catch (err) {
assert.strictEqual(err.message, 'Delay aborted');
}
});
test('Async: timeoutify', async () => {
try {
const request = delay(1000);
await timeoutify(request, 10);
assert.ifError(new Error('Should not be executed'));
} catch (err) {
assert.strictEqual(err.code, 'ETIMEOUT');
assert.strictEqual(err.message, 'Timeout of 10ms reached');
}
try {
const request = delay(10);
const response = await timeoutify(request, 1000);
assert.strictEqual(response, undefined);
} catch {
assert.ifError(new Error('Should not be executed'));
}
});
test('Async: throttle', async () => {
let resolve = null;
const promise = new Promise((r) => {
resolve = r;
});
let callCount = 0;
const fn = (arg1, arg2, ...otherArgs) => {
assert.strictEqual(arg1, 'someVal');
assert.strictEqual(arg2, 4);
assert.deepEqual(otherArgs, []);
callCount++;
assert.ok(callCount <= 2);
if (callCount === 2) resolve();
};
const throttledFn = throttle(1, fn, 'someVal', 4);
throttledFn();
assert.strictEqual(callCount, 1);
throttledFn();
assert.strictEqual(callCount, 1);
throttledFn();
assert.strictEqual(callCount, 1);
return promise;
});
test('Async: throttle merge args', async () => {
let resolve = null;
const promise = new Promise((r) => {
resolve = r;
});
let callCount = 0;
const fn = (arg1, arg2, ...otherArgs) => {
assert.strictEqual(arg1, 'someVal');
assert.strictEqual(arg2, 4);
assert.deepEqual(otherArgs, ['str']);
callCount++;
assert.ok(callCount <= 2);
if (callCount === 2) resolve();
};
const throttledFn = throttle(1, fn, 'someVal', 4);
throttledFn('str');
assert.strictEqual(callCount, 1);
throttledFn('str');
assert.strictEqual(callCount, 1);
throttledFn('str');
assert.strictEqual(callCount, 1);
return promise;
});
test('Async: throttle without arguments', async () => {
let resolve = null;
const promise = new Promise((r) => {
resolve = r;
});
let callCount = 0;
const fn = (...args) => {
assert.deepEqual(args, []);
callCount++;
assert.ok(callCount <= 2);
if (callCount === 2) resolve();
};
const throttledFn = throttle(1, fn);
throttledFn();
assert.strictEqual(callCount, 1);
throttledFn();
assert.strictEqual(callCount, 1);
throttledFn();
assert.strictEqual(callCount, 1);
return promise;
});
test('Async: debounce', async () => {
let resolve = null;
const promise = new Promise((r) => {
resolve = r;
});
let count = 0;
const fn = (arg1, arg2, ...otherArgs) => {
assert.strictEqual(arg1, 'someVal');
assert.strictEqual(arg2, 4);
assert.deepEqual(otherArgs, []);
count++;
assert.strictEqual(count, 1);
resolve();
};
const debouncedFn = debounce(1, fn, 'someVal', 4);
debouncedFn();
assert.strictEqual(count, 0);
debouncedFn();
assert.strictEqual(count, 0);
return promise;
});
test('Async: debounce without arguments', async () => {
let resolve = null;
const promise = new Promise((r) => {
resolve = r;
});
let count = 0;
const fn = (...args) => {
assert.deepEqual(args, []);
count++;
assert.strictEqual(count, 1);
resolve();
};
const debouncedFn = debounce(1, fn);
debouncedFn();
assert.strictEqual(count, 0);
debouncedFn();
assert.strictEqual(count, 0);
return promise;
});
test('Callbackify: Promise to callback-last', async () => {
let resolve = null;
const promise = new Promise((r) => {
resolve = r;
});
const promiseReturning = () => Promise.resolve('result');
const asyncFn = callbackify(promiseReturning);
asyncFn((err, value) => {
assert.ifError(err);
assert.strictEqual(value, 'result');
resolve();
});
return promise;
});
test('Asyncify: sync function to callback-last', async () => {
let resolve = null;
const promise = new Promise((r) => {
resolve = r;
});
const fn = (par) => par;
const asyncFn = asyncify(fn);
asyncFn('result', (err, value) => {
if (err) assert.ifError(err);
assert.strictEqual(value, 'result');
resolve();
});
return promise;
});
test('Promisify: callback-last to Promise', async () => {
const id = 100;
const data = { key: 'value' };
const getDataAsync = (dataId, callback) => {
assert.strictEqual(dataId, id);
callback(null, data);
};
const getDataPromise = promisify(getDataAsync);
try {
const result = await getDataPromise(id);
assert.strictEqual(result, data);
} catch (err) {
assert.ifError(err);
}
});
test('Promisify: callback-last to Promise throw', async () => {
const id = 100;
const getDataAsync = (dataId, callback) => {
assert.strictEqual(dataId, id);
callback(new Error('Data not found'));
};
const getDataPromise = promisify(getDataAsync);
try {
const result = await getDataPromise(id);
assert.notOk(result);
} catch (err) {
assert.ok(err);
}
});