Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 承诺动物园管理员';s多重回调函数_Javascript_Promise_Apache Zookeeper_Bluebird - Fatal编程技术网

Javascript 承诺动物园管理员';s多重回调函数

Javascript 承诺动物园管理员';s多重回调函数,javascript,promise,apache-zookeeper,bluebird,Javascript,Promise,Apache Zookeeper,Bluebird,Zookeeper提供了一个getChildren方法,该方法接受节点的路径并在回调中返回该节点的子节点。它还可以在这个过程中设置一个watch,并在触发watch时调用watcher回调 getChildren(path, function(err,event){ //this is the watcher callback }, function(err,children,stats){ //children callback } )

Zookeeper提供了一个
getChildren
方法,该方法接受节点的路径并在回调中返回该节点的子节点。它还可以在这个过程中设置一个watch,并在触发watch时调用watcher回调

getChildren(path, function(err,event){
        //this is the watcher callback
    },
    function(err,children,stats){
        //children callback
    }
)

因此,如果我使用蓝鸟的
promisify.promisify
来实现这个功能。我如何知道此函数返回的承诺是观察者还是子对象???

如果我正确理解
getChildren()
接口,最后一个回调被设计为使用子对象列表调用一次。第一个回调是一个观察者回调,它可能被无限次调用,以通知您发生的各种更改

有鉴于此,最后一次回调可能符合一个承诺。第一个回调不能且必须保持为回调。此外,第二个回调返回多个结果(这与承诺不完全相符),因此您也必须使用
multiArgs
.spread
来考虑这一点

所以,你可以这样做:

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
});