Home / Function/ pollUntilComplete() — typescript-sdk Function Reference

pollUntilComplete() — typescript-sdk Function Reference

Architecture documentation for the pollUntilComplete() function in async.ts from the typescript-sdk codebase.

Entity Profile

Dependency Diagram

graph TD
  b4433ada_3c32_8526_415a_5f83d7e4401c["pollUntilComplete()"]
  dcc90704_6e64_7ec2_9ee4_aaa5200dbd17["async.ts"]
  b4433ada_3c32_8526_415a_5f83d7e4401c -->|defined in| dcc90704_6e64_7ec2_9ee4_aaa5200dbd17
  f9bbbc22_6269_6b3a_8101_d2e45f03cab9["sleepWithAbort()"]
  b4433ada_3c32_8526_415a_5f83d7e4401c -->|calls| f9bbbc22_6269_6b3a_8101_d2e45f03cab9
  style b4433ada_3c32_8526_415a_5f83d7e4401c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/async.ts lines 198–266

async function pollUntilComplete<T, R extends AsyncEnvelope<T>>(
  apiCall: () => Promise<R>,
  options: AsyncClientOptions
): Promise<T> {
  const {
    timeoutMs = 900000,
    defaultRetryIntervalMs = 10000,
    maxPollingAttempts = 90,
    onPollingProgress,
    signal,
  } = options;

  const startTime = Date.now();
  let attempt = 0;
  let jobId = '';

  while (attempt < maxPollingAttempts) {
    // Check for abort before each attempt
    if (signal?.aborted) {
      const error = new Error('Polling aborted');
      error.name = 'AbortError';
      throw error;
    }

    attempt++;
    const elapsedMs = Date.now() - startTime;

    if (elapsedMs >= timeoutMs) {
      throw new PollingTimeoutError(jobId || 'unknown', timeoutMs, attempt);
    }

    const response = await apiCall();
    jobId = response.jobId;
    const status = response.status;

    if (onPollingProgress) {
      const nextRetryMs = status === 'completed' || status === 'failed' 
        ? undefined 
        : (response.retryAfter || defaultRetryIntervalMs / 1000) * 1000;
      
      onPollingProgress({
        jobId,
        status,
        attempt,
        maxAttempts: maxPollingAttempts,
        elapsedMs,
        nextRetryMs,
      });
    }

    if (status === 'completed') {
      if (response.result !== undefined) {
        return response.result;
      }
      throw new Error(`Job ${jobId} completed but result is undefined`);
    }

    if (status === 'failed') {
      throw new JobFailedError(jobId, response.error || 'Unknown error');
    }

    const retryAfterMs = (response.retryAfter || defaultRetryIntervalMs / 1000) * 1000;
    
    // Use abortable sleep
    await sleepWithAbort(retryAfterMs, signal);
  }

  throw new PollingTimeoutError(jobId || 'unknown', timeoutMs, attempt);
}

Domain

Subdomains

Defined In

Frequently Asked Questions

What does pollUntilComplete() do?
pollUntilComplete() is a function in the typescript-sdk codebase, defined in src/async.ts.
Where is pollUntilComplete() defined?
pollUntilComplete() is defined in src/async.ts at line 198.
What does pollUntilComplete() call?
pollUntilComplete() calls 1 function(s): sleepWithAbort.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free