move helpers to new workspace folder
This commit is contained in:
21
packages/helpers/retry-with-timeout.ts
Normal file
21
packages/helpers/retry-with-timeout.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { setTimeout } from "node:timers/promises"
|
||||
|
||||
const maxTime = 500
|
||||
const waitPeriod = 50
|
||||
|
||||
export async function retryWithTimeout<T>(
|
||||
callback: () => Promise<T> | T,
|
||||
): Promise<T> {
|
||||
const startTime = Date.now()
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
while (true) {
|
||||
try {
|
||||
return await callback()
|
||||
} catch (error) {
|
||||
if (Date.now() - startTime > maxTime) {
|
||||
throw error
|
||||
}
|
||||
await setTimeout(waitPeriod)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user