@adviser/cement - v0.0.0
    Preparing search index...

    Function poller

    • Repeatedly polls an asynchronous function until a terminal state is reached.

      The poller executes the provided function at regular intervals and supports:

      • Exponential backoff for retry intervals
      • Timeouts for individual actions and overall polling
      • Abort signal for cancellation
      • Detailed statistics tracking (attempts, intervals, elapsed time)

      Type Parameters

      • R

        The type of the successful result value

      • CTX = void

      Parameters

      • fn: PollerFunc<R, CTX>

        Function to poll that returns a PollActionResult indicating whether to continue waiting, succeed, error, timeout, or abort

      • ioptions: Partial<PollerOptions<CTX>> = {}

        Optional configuration:

        • intervalMs: Time between poll attempts in milliseconds (default: 1000)
        • timeoutMs: Total timeout in milliseconds, -1 for forever (default: 30000)
        • actionTimeoutMs: Timeout for each individual action attempt, -1 for forever (default: -1)
        • exponentialBackoff: Whether to double the interval after each attempt (default: auto-enabled if timeoutMs is FOREVER)
        • abortSignal: Optional AbortSignal to cancel polling

      Returns Promise<PollerResult<R>>

      Promise resolving to a PollerResult with state, statistics, and result/error/reason depending on the outcome

      const result = await poller(async () => {
      const status = await checkStatus();
      if (status.ready) {
      return { state: 'success', result: status.data };
      }
      return { state: 'waiting' };
      }, { intervalMs: 2000, timeoutMs: 60000 });

      if (result.state === 'success') {
      console.log('Got result:', result.result);
      }