-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
async.js
623 lines (503 loc) · 14.2 KB
/
async.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/**
* Copyright (c) 2014-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var assert = require("assert");
assert(
function*() {}.toString().indexOf("regenerator") !== -1,
"regenerator-transform should be enabled"
);
describe("async functions and await expressions", function() {
Promise = require("promise");
describe("regeneratorRuntime", function() {
it("should be defined globally", function() {
var global = Function("return this")();
assert.ok("regeneratorRuntime" in global);
assert.strictEqual(global.regeneratorRuntime, regeneratorRuntime);
});
it("should have a .wrap method", function() {
assert.strictEqual(typeof regeneratorRuntime.wrap, "function");
});
});
describe("Promise", function() {
it("should be defined globally", function() {
var global = Function("return this")();
assert.ok("Promise" in global);
assert.strictEqual(global.Promise, Promise);
});
it("should be a function", function() {
assert.strictEqual(typeof Promise, "function");
});
});
describe("no-await async function", function() {
it("should return a Promise", function(done) {
var called = false;
async function noAwait(value) {
called = true;
return value;
}
var promise = noAwait("asdf");
assert.strictEqual(called, true);
promise.then(function(value) {
assert.strictEqual(called, true);
assert.strictEqual(value, "asdf");
done();
}).catch(done);
});
});
describe("one-await async function", function() {
it("should finish asynchronously", function(done) {
var flag1 = false;
var flag2 = false;
async function oneAwait(value) {
flag1 = true;
var result = await value;
flag2 = true;
return result;
}
var promise = oneAwait("asdf");
assert.strictEqual(flag1, true);
assert.strictEqual(flag2, false);
promise.then(function(value) {
assert.strictEqual(flag2, true);
assert.strictEqual(value, "asdf");
done();
}).catch(done);
});
});
describe("nested async function calls", function() {
it("should evaluate in the right order", function(done) {
var markers = [];
async function innerMost(marker) {
markers.push(marker);
return await marker;
}
async function inner(marker) {
markers.push(marker);
assert.strictEqual(
await innerMost(marker + 1),
marker + 1
);
markers.push(marker + 2);
assert.strictEqual(
await innerMost(marker + 3),
marker + 3
);
markers.push(marker + 4);
}
async function outer() {
markers.push(0);
await inner(1);
markers.push(6);
await inner(7);
markers.push(12);
}
outer().then(function() {
var expected = [];
for (var i = 0; i <= 12; ++i)
expected.push(i);
assert.deepEqual(markers, expected);
done();
}).catch(done);
});
});
describe("dependent promises", function() {
it("should be awaitable out of order", function(done) {
async function outer(value) {
var resolved = false;
var p1 = new Promise(function(resolve) {
setTimeout(function() {
resolve(value + 1);
resolved = true;
}, 0);
});
assert.strictEqual(resolved, false);
var v2 = await p1.then(function(value) {
return value + 1;
});
assert.strictEqual(resolved, true);
var v1 = await p1;
return [v1, v2];
}
outer(1).then(function(pair) {
assert.deepEqual(pair, [2, 3]);
done();
}).catch(done);
});
});
describe("rejected promises", function() {
it("should cause await expressions to throw", function(done) {
var error = new Error("rejected");
async function f(arg) {
try {
return await arg;
} catch (e) {
assert.strictEqual(e, error);
return "did throw";
}
}
Promise.all([
f(Promise.reject(error)),
f(Promise.resolve("did not throw"))
]).then(function(results) {
assert.deepEqual(results, [
"did throw",
"did not throw"
]);
done();
}).catch(done);
});
it("should be returned by exceptional async functions", function(done) {
var error = new Error("rejected");
async function e(arg) {
if (arg) {
throw arg;
}
return "did not throw";
}
async function f(arg) {
return await e(arg);
}
async function g(arg) {
return await f(arg);
}
async function h(arg) {
return await Promise.all([
g(arg),
Promise.resolve("dummy")
]);
}
Promise.all([
h(error).then(function() {
done(new Error("should not have resolved"));
}, function(e) {
assert.strictEqual(e, error);
return "ok1";
}),
h(null).then(function(result) {
assert.deepEqual(result, [
"did not throw",
"dummy"
]);
return "ok2";
})
]).then(function(results) {
assert.deepEqual(results, ["ok1", "ok2"]);
done();
}).catch(done);
});
it("should propagate failure when returned", function() {
var rejection = new Error("rejection");
async function f() {
return new Promise(function(resolve, reject) {
reject(rejection);
});
}
return f().then(function(result) {
assert.ok(false, "should have been rejected");
}, function(error) {
assert.strictEqual(error, rejection);
});
});
});
describe("async function expressions", function() {
it("should be allowed", function(done) {
(async function(arg) {
return await arg;
})(Promise.resolve(1234)).then(function(value) {
assert.strictEqual(value, 1234);
done();
}).catch(done);
});
});
describe("the this object", function () {
it("should default to undefined (strict)", function (done) {
async function f() {
"use strict";
return this;
}
f().then(function(value) {
assert.strictEqual(value, undefined);
done();
}).catch(done);
});
it("should respect .call's this", function (done) {
async function f() {
return this;
}
var self = {};
f.call(self).then(function(value) {
assert.strictEqual(value, self);
done();
}).catch(done);
});
it("shouldn't capture this when not needed", function () {
// https://github.com/babel/babel/issues/4056
async function f() {
return 0;
}
var source = String(f);
assert.strictEqual(source.indexOf("this"), -1);
});
});
});
describe("async generator functions", function() {
it("should return a working AsyncIterator", function() {
var markers = [];
async function *gen(arg) {
markers.push(0);
var sent = yield arg;
markers.push(1);
var result = await sent;
markers.push(2);
assert.strictEqual(await (yield "second"), "sent after second");
markers.push(3);
return result;
}
var iter = gen("initial argument");
assert.deepEqual(markers, []);
var firstPromise = iter.next();
assert.deepEqual(markers, [0]);
return firstPromise.then(function(firstResult) {
assert.deepEqual(firstResult, {
value: "initial argument",
done: false
});
assert.deepEqual(markers, [0]);
return iter.next(new Promise(function(resolve) {
setTimeout(resolve, 100);
}).then(function() {
assert.deepEqual(markers, [0, 1]);
return "will become final result";
}));
}).then(function(secondResult) {
assert.deepEqual(secondResult, {
value: "second",
done: false
});
assert.deepEqual(markers, [0, 1, 2]);
return iter.next("sent after second");
}).then(function(finalResult) {
assert.deepEqual(markers, [0, 1, 2, 3]);
assert.deepEqual(finalResult, {
value: "will become final result",
done: true
});
});
});
it("should keep results in order", function() {
async function *range(limit) {
var before = [];
var after = [];
for (var i = 0; i < limit; ++i) {
before.push(i);
yield i;
after.push(i);
}
assert.deepEqual(before, after);
return before;
}
var limit = 10;
var iter = range(limit);
var promises = [];
var results = [];
for (var i = 0; i < limit; ++i) {
var promise = iter.next();
promises.push(promise);
promise.then(function(result) {
assert.strictEqual(result.done, false);
results.push(result);
});
}
assert.deepEqual(results, []);
return Promise.all(promises).then(function(promiseResults) {
assert.deepEqual(results, promiseResults);
return iter.next();
}).then(function(finalResult) {
assert.deepEqual(results.map(function(result) {
return result.value;
}), finalResult.value);
assert.strictEqual(finalResult.done, true);
});
});
it("should be able to handle many awaits", function() {
var awaitCount = 0;
function countAwait(i) {
return Promise.resolve(i).then(function() {
++awaitCount;
});
}
async function *gen(limit) {
await countAwait(0);
yield 1;
await countAwait(2);
await countAwait(3);
yield 4;
await countAwait(5);
await countAwait(6);
await countAwait(7);
yield 8;
for (var i = 0; i < limit; ++i) {
await countAwait(i);
}
return "done";
}
var iter = gen(100);
return iter.next().then(function(result) {
assert.strictEqual(awaitCount, 1);
assert.deepEqual(result, {
value: 1,
done: false
});
return iter.next();
}).then(function(result) {
assert.strictEqual(awaitCount, 3);
assert.deepEqual(result, {
value: 4,
done: false
});
return iter.next();
}).then(function(result) {
assert.strictEqual(awaitCount, 6);
assert.deepEqual(result, {
value: 8,
done: false
});
return iter.next();
}).then(function(result) {
assert.strictEqual(awaitCount, 6 + 100);
assert.deepEqual(result, {
value: "done",
done: true
});
return iter.next();
}).then(function(result) {
assert.deepEqual(result, {
value: void 0,
done: true
});
});
});
it("should not propagate exceptions between iterations", function() {
async function *gen() {
yield 1;
yield 2;
}
var iter = gen();
return iter.next().then(function(result) {
assert.deepEqual(result, {
value: 1,
done: false
});
return iter.throw(new Error("thrown from first yield"));
}).then(function() {
throw new Error("should have thrown");
}, function(error) {
assert.strictEqual(error.message, "thrown from first yield");
return iter.next();
}).then(function(result) {
assert.deepEqual(result, {
value: void 0,
done: true
});
});
});
it("should allow yielding a rejected Promise", function() {
var yielded = new Error("yielded rejection");
var returned = new Error("returned rejection");
async function *gen() {
assert.strictEqual(yield "first yielded", "first sent");
try {
assert.strictEqual(yield Promise.reject(yielded), "not reached");
} catch (e) {
assert.strictEqual(yield e, "second sent");
return Promise.reject(returned);
}
}
var iter = gen();
return iter.next().then(function(result) {
assert.deepEqual(result, {
value: "first yielded",
done: false
});
return iter.next("first sent");
}).then(function (result) {
assert.deepEqual(result, {
value: yielded,
done: false
});
return iter.next("second sent");
}).then(function(result) {
assert.ok(false, "should have returned a rejected Promise");
}, function(error) {
assert.strictEqual(error, returned);
});
});
it("should work with nested arrow functions", async function () {
var a = async b => {
return await (async () => {
return await b();
})();
};
assert.strictEqual(
await a(() => Promise.resolve(1234)),
1234
);
});
it("should support super.method(...) in async methods", async function () {
class A {
async method() {
return "from A";
}
}
class B extends A {
async method() {
return "from B " + (await super.method());
}
}
assert.strictEqual(await new B().method(), "from B from A");
});
});
describe("update operators", function() {
it("should read left side before yielding", function() {
let x = 0;
function* test() {
x += yield;
}
var gen = test();
gen.next();
x += 1;
assert.strictEqual(x, 1);
gen.next(2);
assert.strictEqual(x, 2);
});
it("should explode left side before yielding", function() {
let obj = { count: 0 };
function* test() {
obj[yield "key"] += yield "value";
}
var gen = test();
assert.deepEqual(gen.next(), { value: "key", done: false });
assert.strictEqual(obj.count, 0);
assert.deepEqual(gen.next("count"), { value: "value", done: false });
assert.strictEqual(obj.count, 0);
obj.count += 1;
assert.strictEqual(obj.count, 1);
assert.deepEqual(gen.next(2), { value: void 0, done: true });
assert.strictEqual(obj.count, 2);
});
it("should read left side before awaiting", function() {
let x = 0;
async function test(val) {
x += await val;
return x;
}
const promise = test(2);
x += 1;
return promise.then(result => {
assert.strictEqual(result, 2);
});
});
});