-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
59 lines (48 loc) · 1.43 KB
/
index.test.ts
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
import test from "ava";
import {allAsync, applyAsync, sequenceAsync} from "./index";
test("allAsync", async t => {
const a = Promise.resolve(1 as const);
const b = Promise.resolve(2 as const);
const c = Promise.resolve(3 as const);
await allAsync([a, b, c] as const).then(([a, b, c]: readonly [1, 2, 3]) => {
t.is(a, 1);
t.is(b, 2);
t.is(c, 3);
});
function* generate(): Iterable<Promise<number>> {
yield Promise.resolve(1);
yield Promise.resolve(2);
yield Promise.resolve(3);
}
await allAsync(generate()).then(result => t.deepEqual(result, [1, 2, 3]));
});
test("sequenceAsync", async t => {
let a: number[] = [];
const b = async (): Promise<1> =>
Promise.resolve()
.then(() => a.push(1))
.then(() => 1 as const);
const c = async (): Promise<2> => {
a.push(2);
return 2 as const;
};
await sequenceAsync([b, c] as const).then(([b, c]: readonly [1, 2]) => {
t.is(b, 1);
t.is(c, 2);
});
t.deepEqual(a, [1, 2]);
a = [];
function* generate(): Iterable<() => Promise<number>> {
yield b;
yield c;
}
await sequenceAsync(generate()).then(([b, c]) => {
t.is(b, 1);
t.is(c, 2);
});
t.deepEqual(a, [1, 2]);
});
test("applyAsync", async t => {
const f = Promise.resolve((a: number) => a + 1);
t.is(await applyAsync(f)(2), 3);
});