forked from JaoodxD/awaitable-array
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.mjs
59 lines (48 loc) · 1.4 KB
/
test.mjs
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 'node:test'
import { equal } from 'node:assert'
import loadThenables from './loader.mjs'
const thenables = await loadThenables()
for (const thenable of thenables) {
await test(`Primitive ${thenable.name}`, async () => {
// eslint-disable-next-line
Array.prototype.then = thenable
const [a, b] = await [Promise.resolve(1), Promise.resolve(2)]
equal(a, 1)
equal(b, 2)
})
}
for (const thenable of thenables) {
await test(`order ${thenable.name}`, async () => {
// eslint-disable-next-line
Array.prototype.then = thenable
async function check () {
const wait = (ms, value) =>
new Promise(resolve => setTimeout(() => resolve(value), ms))
const [a, b] = await [wait(100, 1), wait(10, 2)]
equal(a, 1)
equal(b, 2)
}
await check()
})
}
for (const thenable of thenables) {
await test(`complex ${thenable.name}`, async () => {
// eslint-disable-next-line
Array.prototype.then = thenable
let expect = 4
let res
const promise = new Promise(resolve => (res = resolve))
async function check () {
const [a, b] = await [Promise.resolve(1), Promise.resolve(2)]
equal(a, 1)
equal(b, 2)
if (--expect >= 0) res()
}
const tick = async () => new Promise(resolve => queueMicrotask(resolve))
check().then(() => check())
check()
await tick()
check()
await promise
})
}