cloudflare--workers-sdk
70bf21e064
Prerelease / build (push) Has been skipped
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
25 行
539 B
TypeScript
25 行
539 B
TypeScript
import { setTimeout } from "node:timers/promises";
|
|
|
|
type MaybePromise<T> = T | Promise<T>;
|
|
|
|
export async function retry<T>(
|
|
retryIf: (currentState: T) => boolean,
|
|
action: () => MaybePromise<T>,
|
|
n = 30
|
|
): Promise<T> {
|
|
const states: T[] = [];
|
|
while (n >= 0) {
|
|
try {
|
|
const currentState = await action();
|
|
if (!retryIf(currentState)) {
|
|
return currentState;
|
|
}
|
|
states.push(currentState);
|
|
} catch {}
|
|
await setTimeout(2_000);
|
|
n--;
|
|
}
|
|
console.error(states);
|
|
throw new Error("Timed out waiting for state to change");
|
|
}
|