Skip to content

Commit 296123d

Browse files
committed
refactor: move timeoutPromise into callAfter helper
1 parent d9f01ae commit 296123d

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/core/asyncUtils.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
AsyncUtils
88
} from '../types'
99

10-
import { resolveAfter } from '../helpers/promises'
10+
import { resolveAfter, callAfter } from '../helpers/promises'
1111
import { TimeoutError } from '../helpers/error'
1212

1313
const DEFAULT_INTERVAL = 50
@@ -40,13 +40,9 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
4040

4141
if (!checkResult()) {
4242
if (timeout) {
43-
const timeoutPromise = new Promise<void>((resolve, reject) => {
44-
if (timeout) {
45-
setTimeout(() => reject(new TimeoutError(wait, timeout)), timeout)
46-
} else {
47-
resolve()
48-
}
49-
})
43+
const timeoutPromise = callAfter(() => {
44+
throw new TimeoutError(wait, timeout)
45+
}, timeout)
5046

5147
await act(() => Promise.race([waitForResult(), timeoutPromise]))
5248
} else {

src/helpers/promises.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
function resolveAfter(ms: number) {
2-
return new Promise((resolve) => {
3-
setTimeout(resolve, ms)
4-
})
2+
return new Promise<void>((resolve) => setTimeout(resolve, ms))
3+
}
4+
5+
export async function callAfter(callback: () => void, ms: number) {
6+
await resolveAfter(ms)
7+
callback()
58
}
69

710
function isPromise<T>(value: unknown): boolean {

0 commit comments

Comments
 (0)