Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.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 承诺如果没有拒绝,结果将是连锁店_Javascript_Node.js_Promise_Angular Promise - Fatal编程技术网

Javascript 承诺如果没有拒绝,结果将是连锁店

Javascript 承诺如果没有拒绝,结果将是连锁店,javascript,node.js,promise,angular-promise,Javascript,Node.js,Promise,Angular Promise,我有一系列的承诺,我用catch来捕捉错误 this.load() .then(self.initialize) .then(self.close) .catch(function(error){ //error-handling }) 如果链已完成且未被拒绝,将调用什么函数? 我使用的是finally,但如果发生错误,也会调用它。 我想在catch函数之后调用一个函数,这个函数只有在没有拒绝任何承诺的情况下才会被调用 我将node.js与q模块

我有一系列的承诺,我用catch来捕捉错误

this.load()
    .then(self.initialize)
    .then(self.close)
    .catch(function(error){
        //error-handling
    })
如果链已完成且未被拒绝,将调用什么函数? 我使用的是finally,但如果发生错误,也会调用它。 我想在catch函数之后调用一个函数,这个函数只有在没有拒绝任何承诺的情况下才会被调用


我将node.js与q模块一起使用。

添加另一个即可

this.load()
    .then(self.initialize)
    .then(self.close)
    .then(function() {
      //Will be called if nothing is rejected
      //for sending response or so
    })
    .catch(function(error){
        //error-handling
    })

我会将您的
.catch()
更改为
.then()
,并提供一个onFullfill和一个onRejected处理程序。然后,您可以准确地确定发生了哪一个,并且您的代码非常清楚其中一个将被执行

this.load()
    .then(self.initialize)
    .then(self.close)
    .then(function() {
         // success handling
     }, function(error){
        //error-handling
     });

仅供参考,这不是做事情的唯一方法。您还可以使用
.then(fn1).catch(fn2)
,它将根据承诺状态的优先级类似地调用fn1或fn2,但如果fn1返回拒绝的承诺或抛出异常,则两者都可能被调用,因为这也将由fn2处理。

感谢jfriend00和Mannu,这两个答案都有效。Thx alot:)只有一个还是另一个@Bergi——正如我所能说的,我的第一个例子是“一个或另一个”。第二个例子可能是两者都有,如果
fn1
抛出或返回一个被拒绝的承诺。是的,是你措辞中的“and”引起了我的注意。