Wait for a task to finish
The
waitForTask
method is only available in thesearch
client context.
Some operations related to the Algolia index are not always instantaneous. Doing such operations with our API clients will provide a taskID
in the response body, so you can later know what is the status of your operation.
We provide a waitForTask
helper method for you to easily wait for a specific task status.
- JavaScript
- PHP
- Java
js
import { algoliasearch } from 'algoliasearch';const client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>');const { taskID } = await client.saveObject({indexName: '<YOUR_INDEX_NAME>',body: {title: 'My Algolia Object',},});// Poll the task status with defaults valuesawait client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID });// Poll the task status with your optionsawait client.waitForTask({indexName: '<YOUR_INDEX_NAME>',taskID,// Number of maximum retries to domaxRetries: 100,// The time to wait between retriestimeout: (retryCount: number): number => Math.min(retryCount * 200, 5000),});
js
import { algoliasearch } from 'algoliasearch';const client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>');const { taskID } = await client.saveObject({indexName: '<YOUR_INDEX_NAME>',body: {title: 'My Algolia Object',},});// Poll the task status with defaults valuesawait client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID });// Poll the task status with your optionsawait client.waitForTask({indexName: '<YOUR_INDEX_NAME>',taskID,// Number of maximum retries to domaxRetries: 100,// The time to wait between retriestimeout: (retryCount: number): number => Math.min(retryCount * 200, 5000),});
php
$client = SearchClient::create('<YOUR_APP_ID>','<YOUR_API_KEY>');$response = $client->saveObject('<YOUR_INDEX_NAME>',['title' => "My Algolia Object"],$requestOptions);// Poll the task status with defaults values$client->waitForTask('<YOUR_INDEX_NAME>', $response['taskID']);// Poll the task status with your optionsAlgolia\AlgoliaSearch\Support\Helpers::retryUntil($client,'getTask',['<YOUR_INDEX_NAME>', $response['taskID'], $requestOptions],function ($res) {return 'published' === $res['status'];},100, // Number of maximum retries to do5000, // Default timeout// Calculation of the time to wait between retriesfunction ($timeout, $retryCount) {return min($retryCount * 200, $timeout) * 1000;});
php
$client = SearchClient::create('<YOUR_APP_ID>','<YOUR_API_KEY>');$response = $client->saveObject('<YOUR_INDEX_NAME>',['title' => "My Algolia Object"],$requestOptions);// Poll the task status with defaults values$client->waitForTask('<YOUR_INDEX_NAME>', $response['taskID']);// Poll the task status with your optionsAlgolia\AlgoliaSearch\Support\Helpers::retryUntil($client,'getTask',['<YOUR_INDEX_NAME>', $response['taskID'], $requestOptions],function ($res) {return 'published' === $res['status'];},100, // Number of maximum retries to do5000, // Default timeout// Calculation of the time to wait between retriesfunction ($timeout, $retryCount) {return min($retryCount * 200, $timeout) * 1000;});
java
import com.algolia.model.search.*;Map<String, Object> body = new HashMap<>();body.put("title", "My Algolia Object");SaveObjectResponse response = client.saveObject("<YOUR_INDEX_NAME>",body);// Poll the task status with defaults valuesawait client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());// Poll the task status with your optionsawait client.waitForTask("<YOUR_INDEX_NAME>",response.getTaskID(),// Number of maximum retries to do100,// The time to wait between retriesretryCount -> {return Math.min(retryCount * 200, 5000);});
java
import com.algolia.model.search.*;Map<String, Object> body = new HashMap<>();body.put("title", "My Algolia Object");SaveObjectResponse response = client.saveObject("<YOUR_INDEX_NAME>",body);// Poll the task status with defaults valuesawait client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());// Poll the task status with your optionsawait client.waitForTask("<YOUR_INDEX_NAME>",response.getTaskID(),// Number of maximum retries to do100,// The time to wait between retriesretryCount -> {return Math.min(retryCount * 200, 5000);});