move helpers to new workspace folder

This commit is contained in:
itsMapleLeaf
2022-10-14 13:22:55 -05:00
parent acbf21842f
commit 0be321b64e
29 changed files with 284 additions and 36 deletions

View File

@@ -0,0 +1,21 @@
import { setTimeout } from "node:timers/promises"
const maxTime = 1000
export async function waitFor<Result>(
predicate: () => Result,
): Promise<Awaited<Result>> {
const startTime = Date.now()
let lastError: unknown
while (Date.now() - startTime < maxTime) {
try {
return await predicate()
} catch (error) {
lastError = error
await setTimeout(50)
}
}
throw lastError ?? new Error("Timeout")
}