Skip to content

Commit c233444

Browse files
authored
Merge pull request #207 from DavidVujic/children2_return_values
fix: handle multiple return values in promisified client
2 parents b265dd0 + ab0737a commit c233444

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ Check out the code in the [examples](./examples) folder: master,workers, tasks a
7878
* `stat = await exists(path, watch)`
7979
* `data = await get(path, watch)`
8080
* `children = await get_children(path, watch)`
81-
* `{children, stat} = await get_children2( path, watch)`
81+
* `[children, stat] = await get_children2( path, watch)`
82+
* return value types:
83+
* children is an array of strings
84+
* stat is an object
8285
* `stat = await set(path, data, version)`
8386
* `val = await sync(path)`
8487
* `delete_ (path, version)`
@@ -92,7 +95,10 @@ Check out the code in the [examples](./examples) folder: master,workers, tasks a
9295
* `stat = await w_exists(path, watch_cb)`
9396
* `data = await w_get(path, watch_cb)`
9497
* `children = await w_get_children(path, watch_cb)`
95-
* `{children, stat} = await w_get_children2 (path, watch_cb)`
98+
* `[children, stat] = await w_get_children2 (path, watch_cb)`
99+
* return value types:
100+
* children is an array of strings
101+
* stat is an object
96102

97103
### Methods: callbacks based client ###
98104

lib/zk_promise.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ class ZooKeeperPromise extends ZooKeeper {
9292
/**
9393
* @param path {string}
9494
* @param watch {boolean}
95-
* @fulfill {Array.<string>>,<stat>}
96-
* @returns {Promise.<Array.<string>>,<stat>}
95+
* @fulfill {Array} [children, stat] - children: an array of strings, stat: object
96+
* @returns {Promise.<Array>} [children, stat] - children: an array of strings, stat: object
9797
*/
9898
get_children2(path, watch) {
9999
return this.promisify(super.a_get_children2, [path, watch]);
@@ -102,8 +102,8 @@ class ZooKeeperPromise extends ZooKeeper {
102102
/**
103103
* @param path {string}
104104
* @param watchCb {function}
105-
* @fulfill {Array.<string>>,<stat>}
106-
* @returns {Promise.<Array.<string>>,<stat>}
105+
* @fulfill {Array} [children, stat] - children: an array of strings, stat: object
106+
* @returns {Promise.<Array>} [children, stat] - children: an array of strings, stat: object
107107
*/
108108
w_get_children2(path, watchCb) {
109109
return this.promisify(super.aw_get_children2, [path, watchCb]);
@@ -166,16 +166,12 @@ class ZooKeeperPromise extends ZooKeeper {
166166
*/
167167
promisify(fn, args) {
168168
return new ZkPromise((resolve, reject) => {
169-
const callback = (rc, error, result) => {
170-
if (rc) {
171-
reject(rc);
172-
} else if (args.length > 3) {
173-
// if there are multiple success values, we return an array
174-
Array.prototype.shift.call(args, 1);
175-
Array.prototype.shift.call(args, 1);
176-
resolve(args);
169+
const callback = (rc, error, ...result) => {
170+
if (rc !== 0) {
171+
reject(new Error(`${rc} ${error}`));
177172
} else {
178-
resolve(result);
173+
const toReturn = result.length === 1 ? result[0] : result;
174+
resolve(toReturn);
179175
}
180176
};
181177
fn.bind(this)(...args, callback);

0 commit comments

Comments
 (0)