Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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
Angularjs 承诺-错误回调与捕获_Angularjs_Q_Angular Promise - Fatal编程技术网

Angularjs 承诺-错误回调与捕获

Angularjs 承诺-错误回调与捕获,angularjs,q,angular-promise,Angularjs,Q,Angular Promise,当使用$q.promise时,有人能告诉我使用错误回调和catch函数之间是否有区别吗 例如,这两段代码在功能上是否等效 function doSomething0() { var deferred = $q.defer(); ... return deferred.promise; } doSomething0() .then(doSomething1) .then(doSomething2) .then(doSomething3)

当使用
$q.promise
时,有人能告诉我使用错误回调和
catch
函数之间是否有区别吗

例如,这两段代码在功能上是否等效

function doSomething0() {
    var deferred = $q.defer();

    ...

    return deferred.promise;
 }

 doSomething0()
    .then(doSomething1)
    .then(doSomething2)
    .then(doSomething3)
    .catch(function (err) {
        // do something with `err`
    });
vs


如果是,为什么要使用第二个呢?在我看来,它看起来更难看,并导致更多的代码重复?

两者将实现相同的效果,但第二个可能会运行三次(而不是一次)。您是对的,它带来了一些代码重复,但它也允许您处理发生的任何错误并继续您的链:

function errorHandler(err) {
  //log error, continue
  return $q.resolve('default value or something');
}

doSomething0()
  .then(doSomething1, errorHandler)
  .then(doSomething2, errorHandler)
  .then(doSomething3, errorHandler);

让我们看一下第一个示例:

doSomething0()
    .then(doSomething1, errorHandler)
    .then(doSomething2, errorHandler)
    .then(doSomething3, errorHandler);


// I've represented functions invocations as if they were synchronous to simplify the example to focus on the error handling
// The above sample is in a way "equivalent" to
try {
    // doSomething0()
    try {
        // doSomething1()
        try {
          // doSomething2()
        } catch(e0) {
           // handle error
        }  
    } catch(e1) {
         // handle error
    }
} catch(e2) {
     // handle error
}
// doSomething3()
但是如果在
doSomething3
处理程序中发生异常,它将不会被处理

好,让我们看一下第二个示例:

doSomething0()
    .then(doSomething1)
    .then(doSomething2)
    .then(doSomething3)
    .catch(function (err) {
        // do something with `err`
    });


// I've represented functions invocations as if they were synchronous to simplify the example to focus on the error handling
// The above sample is in a way "equivalent" to
try {
    // doSomething0()
    // doSomething1()
    // doSomething2()
    // doSomething3()
}
catch(e) {
    // Catch 'em all
    console.log(e)
}

第二个版本允许您通过返回错误或拒绝的承诺以外的任何内容,将承诺恢复到已解析状态,以继续链。如果错误处理程序抛出或返回拒绝的承诺,它将被调用三次。我认为只有当
doSomething3
失败时,
errorHandler
才会被调用三次。但是如果
doSomething2
失败,它将被调用两次,如果
doSomething1
失败,它将被调用一次。谢谢!出于兴趣,我将如何继续使用该链?或者它会继续使用该链吗?@keldar您只需从错误处理函数返回一个新的
Promise
,就像上面的示例一样!不需要回报承诺。您可以简单地返回一个值。“两者将实现相同的目标”。不完全是。请看这个:
doSomething0()
    .then(doSomething1)
    .then(doSomething2)
    .then(doSomething3)
    .catch(function (err) {
        // do something with `err`
    });


// I've represented functions invocations as if they were synchronous to simplify the example to focus on the error handling
// The above sample is in a way "equivalent" to
try {
    // doSomething0()
    // doSomething1()
    // doSomething2()
    // doSomething3()
}
catch(e) {
    // Catch 'em all
    console.log(e)
}