UNPKG

@bigmi/core

Version:

TypeScript library for Bitcoin apps.

39 lines 1.4 kB
import { withRetry } from './withRetry.js'; /** * Retries a condition until it's met or timeout is reached. * Polls the condition function at regular intervals. * * @param condition - Function that returns a truthy value when condition is met * @param options - Configuration options for retry behavior * @returns Promise that resolves to the condition result or undefined if timeout * * @example * // Wait for a value to become available * const value = await retryUntil( * async () => await checkCondition(), * { timeout: 5000, interval: 100 } * ) */ export async function retryUntil(condition, options = {}) { const { timeout = 5000, interval = 100 } = options; const retryCount = Math.ceil(timeout / interval) - 1; // -1 because first attempt is immediate return Promise.race([ // The retry logic withRetry(async () => { const result = await condition(); if (!result) { throw new Error('Condition not met'); } return result; }, { delay: interval, retryCount, shouldRetry: ({ error }) => error.message === 'Condition not met', }).catch(() => undefined), // Timeout fallback new Promise((resolve) => { setTimeout(() => resolve(undefined), timeout); }), ]); } //# sourceMappingURL=retryUntil.js.map