import { setTimeout } from "node:timers/promises"; type MaybePromise = T | Promise; export async function retry( retryIf: (currentState: T) => boolean, action: () => MaybePromise, n = 30 ): Promise { 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"); }