rwsdk
Version:
Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime
32 lines (31 loc) • 976 B
JavaScript
import { setTimeout } from "node:timers/promises";
const POLL_TIMEOUT = process.env.RWSDK_POLL_TIMEOUT
? parseInt(process.env.RWSDK_POLL_TIMEOUT, 10)
: 2 * 60 * 1000;
export async function poll(fn, options = {}) {
const { timeout = POLL_TIMEOUT, interval = 100, minTries = 3, onRetry, } = options;
const startTime = Date.now();
let tries = 0;
while (Date.now() - startTime < timeout || tries < minTries) {
tries++;
try {
if (await fn()) {
return;
}
}
catch (error) {
onRetry?.(error, tries);
// Continue polling on errors
}
await setTimeout(interval);
}
throw new Error(`Polling timed out after ${Date.now() - startTime}ms and ${tries} attempts`);
}
export async function pollValue(fn, options = {}) {
let value;
await poll(async () => {
value = await fn();
return true;
}, options);
return value;
}