Javascript 承诺动物园管理员';s多重回调函数
Zookeeper提供了一个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 } )
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
});