When to wait for tasks
To help manage asynchronous jobs, each method returns a uniquetaskId, which you can use with the waitTask method.
This ensures the job completes before the next request runs.
Some common scenarios where waitTask is useful include:
- Managing dependencies: you want to use
waitTaskto manage dependencies, for example, when deleting an before creating a new one with the same name or clearing an index before adding new . - Atomic reindexing: atomic reindexing is a way to update all your records without any downtime, by populating a temporary index and replacing the destination index with it. You donāt need to implement this by yourself. Instead, you can use the
replaceAllObjectsmethod which does it all for you. - Frontend events: if youāre building a frontend interface that performs indexing operations, you may want to react to completed tasks. For example, you may want to display a success message when an indexing operation has completed, or refresh a page, or move on with some following, co-dependent operations, etc.
- Debugging: use
waitTaskwhen testing a search immediately after updating an index.
When not to wait for tasks
In most scenarios, you donāt need to wait for tasks to complete before moving on with new ones. UsingwaitTask makes your indexing operations synchronous, which slows them down.
Donāt use waitTask instead of your languageās native asynchronous features.
Ensure that tasks are queued in order by using promises or callbacks.