> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Wait for task

> Wait for an index-level task to complete.

Indexing operations are asynchronous.
When you send a request to add or update records,
Algolia queues the operation and returns a `taskID` in the response.
The task runs separately, depending on the server load.

This helper method polls the [Check task status](/doc/rest-api/search/get-task) API and stops if the task's status is `published`.

## Usage

<CodeGroup>
  ```cs C# theme={"system"}
  var response = await client.WaitForTaskAsync("INDEX_NAME", 123L);
  ```

  ```go Go theme={"system"}
  response, err := client.WaitForTask(
    "INDEX_NAME", 123)
  if err != nil {
    // handle the eventual error
    panic(err)
  }
  ```

  ```java Java theme={"system"}
  GetTaskResponse response = client.waitForTask("INDEX_NAME", 123L);
  ```

  ```js JavaScript theme={"system"}
  const response = await client.waitForTask({ indexName: 'wait-task-javascript', taskID: 123 });
  ```

  ```kotlin Kotlin theme={"system"}
  var response = client.waitForTask(indexName = "INDEX_NAME", taskID = 123L)
  ```

  ```php PHP theme={"system"}
  $response = $client->waitForTask(
      'INDEX_NAME',
      123,
  );
  ```

  ```python Python theme={"system"}
  response = client.wait_for_task(
      index_name="INDEX_NAME",
      task_id=123,
  )
  ```

  ```ruby Ruby theme={"system"}
  response = client.wait_for_task("INDEX_NAME", 123)
  ```

  ```scala Scala theme={"system"}
  val response = Await.result(
    client.waitForTask(
      indexName = "INDEX_NAME",
      taskID = 123L
    ),
    Duration(100, "sec")
  )
  ```

  ```swift Swift theme={"system"}
  let response = try await client.waitForTask(indexName: "INDEX_NAME", taskID: Int64(123))
  ```
</CodeGroup>

## Parameters

<Tabs>
  <Tab title="C#">
    <ParamField body="indexName" type="string" required>
      Index name where the task was executed.
    </ParamField>

    <ParamField body="taskId" type="long" required>
      Task ID to wait for.
    </ParamField>

    <ParamField body="maxRetries" type="int" default={50}>
      Maximum number of retries for checking the status.
    </ParamField>

    <ParamField body="timeout" type="Func<int,int>">
      Returns a timeout based on the current number of iterations.
    </ParamField>

    <ParamField body="requestOptions" type="RequestOptions">
      Additional [request options](/doc/libraries/sdk/request-options).
    </ParamField>

    <ParamField body="cancellationToken" type="CancellationToken" default="default">
      Parameter that can be used as a signal to cancel this request.
    </ParamField>
  </Tab>

  <Tab title="Dart">
    <ParamField body="indexName" type="string" required>
      Index name where the task was executed.
    </ParamField>

    <ParamField body="taskID" type="int" required>
      Task ID to wait for.
    </ParamField>

    <ParamField body="params" type="WaitParams">
      <Expandable defaultOpen>
        <ParamField body="maxRetries" type="int" default={50}>
          Maximum number of retries for checking the status.
        </ParamField>

        <ParamField body="timeout" type="Duration">
          Timeout in milliseconds after which an exception is thrown.
        </ParamField>

        <ParamField body="initialDelay" type="Duration" default="200 ms">
          Initial delay after which to check for the status for the first time.
        </ParamField>

        <ParamField body="maxDelay" type="Duration" default="5 s">
          Maximum delay between status checks.
        </ParamField>
      </Expandable>
    </ParamField>

    <ParamField body="requestOptions" type="RequestOptions">
      Additional [request options](/doc/libraries/sdk/request-options).
    </ParamField>
  </Tab>

  <Tab title="Go">
    <ParamField body="indexName" type="string" required>
      Index name where the task was executed.
    </ParamField>

    <ParamField body="taskID" type="int64" required>
      Task ID to wait for.
    </ParamField>

    <ParamField body="opts..." type="IterableOption">
      Functional options to provide extra arguments.

      <Expandable title="available functions">
        <ParamField body="search.WithMaxRetries" type="function">
          **Signature:** `func(maxRetries int) iterableOption`

          Sets the maximum number of retries for this method.
        </ParamField>

        <ParamField body="search.WithTimeout" type="function">
          **Signature:** `func(count int) time.Duration`

          Returns a timeout based on the current number of iterations.
        </ParamField>

        <ParamField body="search.WithHeaderParam" type="function">
          **Signature:** `func(key string, value string) requestOption`

          Sets extra header parameters for this request.
          To learn more, see [request options](/doc/libraries/sdk/request-options).
        </ParamField>

        <ParamField body="search.WithQueryParam" type="function">
          **Signature:** `func(key string, value string) requestOption`

          Sets extra query parameters for this request.
          To learn more, see [request options](/doc/libraries/sdk/request-options).
        </ParamField>
      </Expandable>
    </ParamField>
  </Tab>

  <Tab title="Java">
    <ParamField body="indexName" type="String" required>
      Index name where the task was executed.
    </ParamField>

    <ParamField body="taskID" type="long" required>
      Task ID to wait for.
    </ParamField>

    <ParamField body="maxRetries" type="int" default={50}>
      Maximum number of retries for checking the status.
    </ParamField>

    <ParamField body="timeout" type="function">
      **Default:** `(int retries) -> Math.min(retries * 200, 5000)`

      Returns the time between status checks based on the current number of iterations.
      By default, the initial delay is 200 milliseconds.
      With every iteration, the delay increases by 200 milliseconds,
      until a maximum of 5 seconds is reached.
    </ParamField>

    <ParamField body="requestOptions" type="RequestOptions">
      Additional [request options](/doc/libraries/sdk/request-options).
    </ParamField>
  </Tab>

  <Tab title="JavaScript">
    <ParamField body="indexName" type="string" required>
      Index name where the task was executed.
    </ParamField>

    <ParamField body="taskID" type="number" required>
      Task ID to wait for.
    </ParamField>

    <ParamField body="maxRetries" type="number" default={50}>
      Maximum number of retries for checking the status.
    </ParamField>

    <ParamField body="timeout" type="function">
      **Default:** `(retryCount: number) -> Math.min(retryCount * 200, 5000)`

      Returns the time between status checks based on the current number of iterations.
      By default, the initial delay is 200 milliseconds.
      With every iteration, the delay increases by 200 milliseconds,
      until a maximum of 5 seconds is reached.
    </ParamField>

    <ParamField body="requestOptions" type="RequestOptions">
      Additional [request options](/doc/libraries/sdk/request-options).
    </ParamField>
  </Tab>

  <Tab title="Kotlin">
    <ParamField body="indexName" type="String" required>
      Index name where the task was executed.
    </ParamField>

    <ParamField body="taskID" type="Long" required>
      Task ID to wait for.
    </ParamField>

    <ParamField body="maxRetries" type="int" default={50}>
      Maximum number of retries for checking the status.
    </ParamField>

    <ParamField body="initialDelay" type="Duration" default="200 ms">
      Time after which the status is checked for the first time.
      This time is doubled after every check until it reaches the value set by `maxDelay`.
    </ParamField>

    <ParamField body="maxDelay" type="Duration" default="5 seconds">
      Maximum time to wait between status checks.
    </ParamField>

    <ParamField body="requestOptions" type="RequestOptions">
      Additional [request options](/doc/libraries/sdk/request-options).
    </ParamField>
  </Tab>

  <Tab title="PHP">
    <ParamField body="indexName" type="string" required>
      Index name where the task was executed.
    </ParamField>

    <ParamField body="taskId" type="int" required>
      Task ID to wait for.
    </ParamField>

    <ParamField body="maxRetries" type="int" default={50}>
      Maximum number of retries for checking the status.
    </ParamField>

    <ParamField body="timeout" type="int" default={5000}>
      Number of milliseconds between status checks.
    </ParamField>

    <ParamField body="requestOptions" type="array">
      Additional [request options](/doc/libraries/sdk/request-options).
    </ParamField>
  </Tab>

  <Tab title="Python">
    <ParamField body="index_name" type="str" required>
      Index name where the task was executed.
    </ParamField>

    <ParamField body="task_id" type="int" required>
      Task ID to wait for.
    </ParamField>

    <ParamField body="max_retries" type="int" default={50}>
      Maximum number of retries for checking the status.
    </ParamField>

    <ParamField body="timeout" type="Callable[[int], int]">
      **Default:** `min(retry_count*0.2, 5)`

      Returns the time between status checks based on the current number of iterations.
      By default, the initial delay is 200 milliseconds.
      With every iteration, the delay increases by 200 milliseconds,
      until a maximum of 5 seconds is reached.
    </ParamField>

    <ParamField body="request_options" type="dict | RequestOptions">
      Additional [request options](/doc/libraries/sdk/request-options).
    </ParamField>
  </Tab>

  <Tab title="Ruby">
    <ParamField body="index_name" type="String" required>
      Index name where the task was executed.
    </ParamField>

    <ParamField body="task_id" type="Integer" required>
      Task ID to wait for.
    </ParamField>

    <ParamField body="max_retries" type="Integer" default={50}>
      Maximum number of retries for checking the status.
    </ParamField>

    <ParamField body="timeout" type="function">
      **Default:** `[retry_count * 200, 5000].min`

      Returns the time between status checks based on the current number of iterations.
      By default, the initial delay is 200 milliseconds.
      With every iteration, the delay increases by 200 milliseconds,
      until a maximum of 5 seconds is reached.
    </ParamField>

    <ParamField body="request_options" type="Hash">
      Additional [request options](/doc/libraries/sdk/request-options).
    </ParamField>
  </Tab>

  <Tab title="Scala">
    <ParamField body="indexName" type="String" required>
      Index name where the task was executed.
    </ParamField>

    <ParamField body="taskID" type="Long" required>
      Task ID to wait for.
    </ParamField>

    <ParamField body="maxRetries" type="int" default={50}>
      Maximum number of retries for checking the status.
    </ParamField>

    <ParamField body="delay" type="Long => Long">
      **Default:** `(retries: Long) => Math.min(retries * 200, 5000)`

      Returns the time between status checks based on the current number of iterations.
      By default, the initial delay is 200 milliseconds.
      With every iteration, the delay increases by 200 milliseconds,
      until a maximum of 5 seconds is reached.
    </ParamField>

    <ParamField body="requestOptions" type="RequestOptions">
      Additional [request options](/doc/libraries/sdk/request-options).
    </ParamField>
  </Tab>

  <Tab title="Swift">
    <ParamField body="indexName" type="String" required>
      Index name where the task was executed.
    </ParamField>

    <ParamField body="taskID" type="Int64" required>
      Task ID to wait for.
    </ParamField>

    <ParamField body="maxRetries" type="int" default={50}>
      Maximum number of retries for checking the status.
    </ParamField>

    <ParamField body="timeout" type="(int) -> TimeInterval">
      **Default:** `(retryCount) -> min(TimeInterval(retryCount) * 0.2, 5)`

      Returns the time between status checks based on the current number of iterations.
      By default, the initial delay is 200 milliseconds.
      With every iteration, the delay increases by 200 milliseconds,
      until a maximum of 5 seconds is reached.
    </ParamField>

    <ParamField body="requestOptions" type="RequestOptions">
      Additional [request options](/doc/libraries/sdk/request-options).
    </ParamField>
  </Tab>
</Tabs>
