mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-09 05:19:32 +08:00
28 lines
629 B
TypeScript
28 lines
629 B
TypeScript
export type PollOptions = {
|
|
timeoutMs?: number;
|
|
intervalMs?: number;
|
|
};
|
|
|
|
function sleep(ms: number) {
|
|
return new Promise<void>((resolve) => setTimeout(resolve, ms));
|
|
}
|
|
|
|
export async function pollUntil<T>(
|
|
fn: () => Promise<T | null | undefined>,
|
|
opts: PollOptions = {},
|
|
): Promise<T | undefined> {
|
|
const timeoutMs = opts.timeoutMs ?? 2000;
|
|
const intervalMs = opts.intervalMs ?? 25;
|
|
const start = Date.now();
|
|
|
|
while (Date.now() - start < timeoutMs) {
|
|
const value = await fn();
|
|
if (value !== null && value !== undefined) {
|
|
return value;
|
|
}
|
|
await sleep(intervalMs);
|
|
}
|
|
|
|
return undefined;
|
|
}
|