Zookeeper provides a getChildren
method that takes in the path of the node and returns the children of that node in a callback. It also sets a watch during the process and calls the watcher callback when a watch is triggered
getChildren(path, function(err,event){
//this is the watcher callback
},
function(err,children,stats){
//children callback
}
)
So if I use the bluebird's Promise.promisify
to promisify this function. How do I know that the promise this function is returning is the watcher or the children ??
If I understand the getChildren()
interface correctly, the last callback is designed to be called once with the list of child objects. The first callback is a watcher callback that may be called an indefinite number of times to notify you of various changes occurring.
Given that, the last callback could fit with a promise. The first callback cannot and must remain a callback. In addition, the second callback is returning multiple results (which does not fit perfectly with promises) so you have to make allowances for that too using multiArgs
and .spread
.
So, you could do something like this:
let getChildrenP = Promise.promisify(getChildren, {multiArgs: true});
getChildrenP(path, function(err, event) {
// watcher event occurred here
// this may get called multiple times
}).spread(function(children, stats) {
// children are available now
}).catch(function(err) {
// error here
});