Skip to content

Asynchronous API

The Aerospike Node.js client supports Node.js-style callbacks as well as Promises for all asynchronous database operations. This tutorial demonstrates both methods of asynchronous client operations.

Example

This example demonstrates how to write a large set of records asynchronously. You can also use other database calls such as Batch, Delete and Scan asynchronously.

import pLimit from "p-limit";
const Aerospike = await import("aerospike");
const limit = pLimit(100);
const recordMax = 100000;
// Set hosts to your server's address and port
const config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Establishes a connection to the server
const client = await Aerospike.connect(config);
const promises = [];
for (let i = 0; i < recordMax; i++) {
promises.push(
limit(async () => {
const key = new Aerospike.Key("test", "demo", i);
const bins = { name: "example", age: 31, id: i };
return client.put(key, bins);
}),
);
}
const results = await Promise.allSettled(promises);
for (const { status, reason } of results) {
if (status === "rejected") {
console.error("write failed with reason: \n" + reason);
}
}
await client.close();
Feedback