Skip to content

    waitForRun

    Poll an accepted run until it finishes or needs approval. For Brave submissions, the helper returns at submitted or failed.

    Signature

    waitForRun(
      client: RunPoller,
      target: string,
      opts?: WaitOptions,
    ): Promise<Record<string, unknown>>

    Parameters

    client
    An AeoClient, or an object with baseUrl and v2GetRunsRunId.
    target
    A qualified run ID or its poll URL on the configured API origin. For a poll URL with a query string, use the qualified run ID and pass the brand ID separately. Read estimates directly.
    opts.brandId
    Required for a saved check. Sent as brand_id on each poll request.
    opts.timeoutMs
    Defaults to 600000 milliseconds. After each unfinished poll, the helper checks whether there is time for the next wait. A request already in progress can run past this deadline.
    opts.initialIntervalMs, opts.maxIntervalMs, opts.factor
    The first interval is 1000 ms. Each wait grows by a factor of 1.6 up to 30000 ms, unless you override these values.
    opts.stopOnAction
    Defaults to true. Set false to continue polling when the state is awaiting_approval.
    opts.signal
    Pass an AbortSignal to cancel an active HTTP request. If you abort during the wait between polls, the next request receives the aborted signal when the wait ends.
    opts.sleep, opts.now
    Optional functions to control elapsed time and delays in tests.

    Return value

    The run object from body.data. Check its state or status before using the result. A failed run returns its record.

    When to stop polling

    Brave submissions
    The helper returns when data.status is submitted (Brave accepted the URL) or failed. To poll while the status is in_progress yourself, use the Brave polling example.
    Collection and other jobs
    Check data.state for completed, partial, failed or cancelled. Managed runs use data.status.
    Jobs that need approval
    By default the helper returns at awaiting_approval so you can request a decision. It also returns at rejected or stale_approval.
    Retrieval simulations
    A successful run returns data.status of succeeded. Stop on failed or cancelled as well.
    Accuracy checks and cited-page runs
    Both report in_progress until data.status reaches completed. A cited-page sweep or labelling run can also end at cancelled.
    Saved checks
    Pass the brand ID as opts.brandId. The helper checks data.check.state and returns the full data object, including check and the page of recorded prompts.
    Analyst reports
    The helper returns when data.report_id is present.
    Estimates
    Read the estimate directly. It has an expiry time and requires no polling.

    Errors

    • RunTimeoutError when the next wait would exceed the deadline. Its run property contains the last result.
    • Error for an invalid ID or poll URL. An API error or network failure while polling also reaches the caller.

    TypeScript example

    import { waitForRun } from "@aeoapi/aeo-sdk";
    import { AeoClient } from "@aeoapi/aeo-sdk";
    
    const apiKey = process.env.AEO_API_KEY;
    if (!apiKey) throw new Error("Set AEO_API_KEY on your server.");
    const client = new AeoClient(apiKey, { baseUrl: "https://api.aeoapi.dev" });
    
    // Replace this ID with the qualified run ID returned by your start request.
    const run = await waitForRun(client, "job:00000000-0000-4000-8000-000000000001", {
      timeoutMs: 120_000,
    });
    console.log(run.state ?? run.status);

    See TypeScript client setup for authentication, timeouts and retry settings.