Constructor
new AsyncClient(client)
Creates a new AsyncClient for communicating with zookeeper. This object will
expose all the methods of the underlying zookeeper client, but also expose
a number of methods that return promises.
Parameters:
Name | Type | Description |
---|---|---|
client |
Client | the underlying zookeeper client created by createClient. |
Example
const zk = require('node-zookeeper-client-async');
const client = zk.createAsyncClient("127.0.0.1:2181");
// connect to the server
await client.connectAsync();
console.log('connected!');
// create a node
const rootPath = await client.mkdirpAsync('/test');
console.log(`created ${rootPath}`)
// add some ephemeral nodes
await client.createAsync('/test/counter-', Buffer.from('first'), null, zk.CreateMode.EPHEMERAL_SEQUENTIAL);
await client.createAsync('/test/counter-', Buffer.from('second'), null, zk.CreateMode.EPHEMERAL_SEQUENTIAL);
// list the nodes
const nodes = await client.getChildrenAsync('/test');
// print stuff to console
console.log(`${rootPath} has the children:`)
await Promise.all(nodes.map(async node => {
const data = await client.getDataAsync(`/test/${node}`);
console.log(` ${node}: ${data.data}`);
}));
// delete everything
await client.rmrfAsync(rootPath);
// shut down
await client.closeAsync();
console.log('disconnected');
Methods
closeAsync() → {Promise}
Close this client. Once the client is closed, its session becomes invalid. All the
ephemeral nodes in the ZooKeeper server associated with the session will be
removed. The watchers left on those nodes (and on their parents) will be triggered.
Resolves true when the client disconnects, resolves false if the client is already
disconnected.
Returns:
resolves when disconnected. Does not reject.
- Type
- Promise
connectAsync() → {Promise}
Initiate the connection to the provided server list (ensemble). The client will
pick an arbitrary server from the list and attempt to connect to it. If the
establishment of the connection fails, another server will be tried (picked
randomly) until a connection is established or close method is invoked.
Returns:
resolves on connect. Rejects with message on failure.
- Type
- Promise
createAsync(path, data, acls, mode) → {Promise}
Create a node with given path, data, acls and mode. Resolves the path name if
successful, resolves false if the path already exists, and rejects for anything
else going wrong
Parameters:
Name | Type | Default | Description |
---|---|---|---|
path |
String | Path of the node. | |
data |
Buffer | null | The data buffer, optional, defaults to null. |
acls |
ACL | null | An array of ACL objects, optional, defaults to ACL.OPEN_ACL_UNSAFE. |
mode |
CreateMode | null | The creation mode, optional, defaults to CreateMode.PERSISTENT. |
Returns:
resolves with the path of the created item. Rejects with an error
if something goes wrong.
- Type
- Promise
existsAsync(path) → {Promise}
For the given node path, retrieve the children list and the stat. The children will
be an unordered list of strings. Resolves the stat object if the node exists,
resolves null if it does not exist, rejects if anything goes wrong.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | the Path of the node. |
Returns:
- Type
- Promise
getACLAsync(path) → {Promise}
Retrieve the list of ACL and stat of the node of the given path. Will resolve an
object with acls as a list of ACL objects and stat as the stat for the node. Will
resolve null if the node does not exist and will reject if anything goes wrong.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | the Path of the node. |
Returns:
- Type
- Promise
getChildrenAsync(path) → {Promise}
For the given node path, retrieve the children list and the stat. The children will
be an unordered list of strings. Resolved the children if the node exists, resolves
null if the node does not exist. Rejects if anything goes wrong.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | the Path of the node. |
Returns:
- Type
- Promise
getDataAsync(path) → {Promise}
Retrieve the data and the stat of the node of the given path. Resolves an object
containing data as a Buffer object and stat as the stat object. Resolves null
if the node does not exist. Rejects if anything goes wrong.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | the Path of the node. |
Returns:
- Type
- Promise
mkdirpAsync(path, data, acls, mode) → {Promise}
Create given path in a way similar to mkdir -p. Will resolve the path if the node
is created and will reject if anything goes wrong.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
path |
string | the Path of the node. | |
data |
Buffer | null | The data buffer, optional, defaults to null. |
acls |
Array.<ACL> | null | array of ACL objects, optional, defaults to ACL.OPEN_ACL_UNSAFE |
mode |
CreateMode | null | The creation mode, optional, defaults to CreateMode.PERSISTENT |
Returns:
- Type
- Promise
removeAsync(path, version) → {Promise}
Delete a node with the given path and version. If version is provided and not equal
to -1, the request will fail when the provided version does not match the server
version. Resolves true if the node is deleted. Resolves false if the node does
not exist. Rejects if the node has children, if the version does not match, or
for any other result.
Parameters:
Name | Type | Description |
---|---|---|
path |
String | Path of the node. |
version |
Number | The version of the node, optional, defaults to -1. |
Returns:
- Type
- Promise
rmrfAsync(path) → {Promise}
Remove a given path in a way similar to rm -rf. Will resolve if the node is deleted
or does not exist and reject if anything goes wrong.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | the Path of the node. |
Returns:
- Type
- Promise
setACLAsync(path, acls, version)
Set the ACL for the node of the given path if such a node exists and the given
version (optional) matches the version of the node on the server. (if the given
version is -1, it matches any versions). Will resolve on success and reject
if anything goes wrong or the node does not exist.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | the Path of the node. |
acls |
Array.<ACL> | An array of ACL instances to set on the node. |
version |
Number | the version to set. -1 (default) to match any version. |
setDataAsync(path, data, version) → {Promise}
Set the data for the node of the given path if such a node exists and the optional
given version matches the version of the node (if the given version is -1, it
matches any node's versions). Will resolve the stat of the node if successful. Will
reject if unsuccessful or if the node does not exist.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | the Path of the node. |
data |
Buffer | the data to set on the node. |
version |
Number | the version to set. -1 (default) to match any version. |
Returns:
- Type
- Promise
transaction() → {AsyncTransaction}
Create and return a new Transaction instance which provides a builder object that
can be used to construct and commit a set of operations atomically.
Returns:
- Type
- AsyncTransaction