This version of the JavaScript API client has been deprecated in favor of the latest version of the JavaScript API client.
Required API Key: any key with the addObject
ACL
About this method
Wait for a task to complete before executing the next line of code, to synchronize index updates.
All write operations in Algolia are asynchronous by design.
It means that when you add or update an object to your index, our servers will
reply to your request with a taskID
as soon as they understood the write operation.
The actual insert and indexing will be done after replying to your code.
You can wait for a task to complete by using the `taskID’ and this method.
Check out our full discussion about asynchronous methods.
Examples
Wait for indexing of a new object:
1
2
3
4
5
6
| $index->addObject(
[
'firstname' => 'Jimmie',
'lastname' => 'Barninger'
]
)->wait();
|
1
2
3
4
5
6
| # Asynchronous call and wait_task
res = index.add_object({ firstname: 'Jimmie', lastname: 'Barninger' })
index.wait_task(res['taskID'])
# Synchronous call, that does the wait_task internally
index.add_object!({ firstname: 'Jimmie', lastname: 'Barninger' })
|
1
2
3
4
5
6
7
8
9
10
11
12
| const object = {
firstname: 'Jimmie',
lastname: 'Barninger'
};
index.addObject(object, (err, { taskID, objectID } = {}) => {
index.waitTask(taskID, err => {
if (!err) {
console.log(`object ${objectID} indexed`);
}
});
});
|
1
2
3
4
| index.save_object({
'firstname': 'Jimmie',
'lastname': 'Barninger'
}, {'autoGenerateObjectIDIfNotExist': True}).wait()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| index.addObject(
newObject,
completionHandler: { (content, error) -> Void in
if error != nil {
return
}
guard let taskID = content!["taskID"] as? Int else {
return // could not retrieve task ID
}
index.waitTask(
withID: taskID,
completionHandler: { (content, error) -> Void in
if error == nil {
print("New object is indexed!")
}
}
)
}
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| JSONObject object = new JSONObject()
.put("firstname", "Jimmie")
.put("lastname", "Barninger");
index.addObjectAsync(object, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
if (error != null) {
// Handle error.
} else {
String taskID = content.optString("taskID", null);
if (taskID == null) {
// Handle error.
} else {
index.waitTaskAsync(taskID, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
if (error == null) {
// Task is published.
}
}
});
}
}
}
});
|
1
2
3
4
5
6
7
8
9
10
11
| Contact contact = new Contact
{
Firstname = "Jimmie",
Lastname = "Barninger"
};
index.SaveObject(contact, autoGenerateObjectId: true).Wait();
// Asynchronous
var resp = await index.SaveObjectsAsync(contacts, autoGenerateObjectId: true);
resp.Wait();
|
1
2
3
4
5
6
7
| BatchIndexingResponse resp = index.saveObject(
new Contact().setFirstname("Jimmie").setLastname("Barninger")
);
// All objects implementing the AlgoliaWaitableResponse interface
// Can be awaited with the .waitTask() method
resp.waitTask();
|
1
2
3
4
5
6
7
8
9
10
11
12
| type Contact struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
contact := Contact{
Firstname: "Jimmie",
Lastname: "Barninger",
}
res, err := index.SaveObject(contact, opt.AutoGenerateObjectIDIfNotExist(true))
err = res.Wait()
|
1
2
3
4
| for {
t <- client.execute { index into "toto" `object` MyObject("test") }
r <- client.execute { waitFor task t from "toto" }
} yield "indexing is done"
|
1
2
3
4
5
6
7
8
| val json = json {
"Firstname" to "Jimmie"
"Lastname" to "Barninger"
}
index.apply {
saveObject(json).wait()
}
|
If you want to ensure multiple objects have been indexed, you need to check
all taskID
s.
Wait for indexing of a new object and send extra http header
1
2
3
4
5
6
7
8
| $index->addObject(
[
'firstname' => 'Jimmie',
'lastname' => 'Barninger'
]
)->wait([
'X-Forwarded-For' => '94.228.178.246'
]);
|
1
2
3
4
5
6
7
| extra_headers = {
'X-Algolia-User-ID': 'user123'
}
time_before_retry = 100
index.wait_task(res['taskID'], time_before_retry, extra_headers)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| client.setExtraHeader('X-Forwarded-For', '94.228.178.246');
const object = {
firstname: 'Jimmie',
lastname: 'Barninger'
};
index.addObject(object, (err, { taskID, objectID } = {}) => {
index.waitTask(taskID, err => {
if (!err) {
console.log(`object ${objectID} indexed`);
}
});
});
|
1
2
3
4
5
6
| index.save_object({
'firstname': 'Jimmie',
'lastname': 'Barninger'
}, {'autoGenerateObjectIDIfNotExist': True}).wait({
'X-Forwarded-For': '94.228.178.246'
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| let requestOptions = RequestOptions()
requestOptions.headers["X-Algolia-User-ID"] = "user123"
index.addObject(
newObject,
requestOptions: requestOptions,
completionHandler: { (content, error) -> Void in
if error != nil {
return
}
guard let taskID = content!["taskID"] as? Int else {
return // could not retrieve task ID
}
index.waitTask(
withID: taskID,
completionHandler: { (content, error) -> Void in
if error == nil {
print("New object is indexed!")
}
}
)
}
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| JSONObject object = new JSONObject()
.put("firstname", "Jimmie")
.put("lastname", "Barninger");
index.addObjectAsync(object, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
if (error != null) {
// Handle error.
} else {
String taskID = content.optString("taskID", null);
if (taskID == null) {
// Handle error.
} else {
index.waitTaskAsync(taskID, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
if (error == null) {
// Task is published.
}
}
});
}
}
}
});
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| List<Contact> contacts = new List<Contact>
{
new Contact
{
ObjectID = "myID1",
Firstname = "Jimmie",
Lastname = "Barninger"
},
new Contact
{
ObjectID = "myID2",
Firstname = "Warren",
Lastname = "Speach"
}
};
RequestOptions requestOptions = new RequestOptions
{
Headers = new Dictionary<string,string>{ { "X-Algolia-User-ID", "user123" } }
};
index.SaveObjects(contacts, requestOptions).Wait();
// Asynchronous
var resp = await index.SaveObjectsAsync(contacts, requestOptions);
resp.Wait();
|
1
2
3
4
5
6
7
8
| BatchIndexingResponse resp = index.saveObject(
new Contact().setFirstname("Jimmie").setLastname("Barninger"),
new RequestOptions().addExtraHeader("X-Algolia-User-ID", "user123")
);
// All objects implementing the AlgoliaWaitableResponse interface
// Can be awaited with the .waitTask() method
resp.waitTask();
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| type Contact struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
contact := Contact{
Firstname: "Jimmie",
Lastname: "Barninger",
}
opts := []interface{}{
opt.AutoGenerateObjectIDIfNotExist(true),
opt.ExtraHeaders(map[string]string{"X-Algolia-User-ID": "userID2"}),
}
res, err := index.SaveObject(contact, opts...)
err = res.Wait()
|
1
2
3
4
5
6
7
8
| for {
t <- client.execute { index into "toto" `object` MyObject("test") }
r <- client.execute {
waitFor task t from "toto" options RequestOptions(
extraHeaders = Some(Map("X-Algolia-User-ID" => "user123"))
)
}
} yield "indexing is done"
|
Parameters
taskID
|
taskID of the indexing task to wait for.
|
requestOptions
|
type: list
default: No requestOptions
Optional
A list of request options to send along with the query.
|