When to wait for tasks
To help manage asynchronous jobs, each method returns a uniquetaskId
which you can use with the waitTask
method. Using this method guarantees that a job has finished before proceeding with a new request. There are a few situations when this comes in handy:
- Managing dependencies: you want to use
waitTask
to manage dependencies, for example, when deleting an index before creating a new index with the same name or clearing an index before adding new objects. - 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
replaceAllObjects
method 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-dependant operations, etc.
- Debugging: You also most often need
waitTask
in debugging scenarios, when youβre 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, thus slows them down.
Conversely, you donβt need to use waitTask
in place of the asynchronous mechanisms of your programming language. You can ensure that tasks are queued in order by using promises or callbacks.