vs deferred.reject角的q.reject美元()()

时间:2022-08-23 11:37:30

I'm trying to get a handle on the Angular $q service and its related objects and APIs. When I look at the objects in my console I see:

我正在尝试获取角$q服务及其相关对象和api的句柄。当我看着我的控制台里的对象时,我看到:

var deferred = $q.defer()

...(and then from console inspection)...

$q: Object {defer: function, reject: function, when: function, all: function}

deferred: Object {resolve: function, reject: function, notify: function, promise: Object}

deferred.promise: Object {then: function, catch: function, finally: function}

It raises a few questions:

它提出了几个问题:

  1. What's the difference between $q.reject() and deferred.reject()? When to use each?
  2. $q.reject()和deferred.reject()有什么区别?何时使用?
  3. What's the relationship between the errorFn in deferred.promise.then(successFn, errorFn) and the catchFn in deferred.promise.catch(catchFn)?
  4. 延期合同中的错误和承诺之间的关系是什么?然后(successFn, errorFn)和catch(catch)是什么意思?
  5. If I have a bunch of nested promises and an error occurs, will the outermost catch() function always be called? What if one of the nested promises also has a catch function defined? Will that catch prevent the outermost catch from executing?
  6. 如果我有一堆嵌套的承诺并且发生了错误,那么最外层的catch()函数是否总是被调用?如果一个嵌套的承诺也定义了catch函数呢?那样的捕捉会阻止最外的捕捉吗?

Thanks.

谢谢。

2 个解决方案

#1


89  

1) $q.reject() is a shortcut to create a deferred and then reject it immediately; I often use this in an errorFn if I can't handle the error.

1) $q.reject()是创建递延后立即拒绝的快捷方式;如果我处理不了这个错误,我经常会用这个。

2) Nothing, promise.catch(errorFn) is just syntactic sugar for promise.then(null, errorFn), just like the success and error methods of the $http service, so you can write code like the following:

没什么,承诺。catch(错误)只是语法上的承诺。然后(null, errorFn),就像$http服务的成功和错误方法一样,您可以编写如下代码:

promise.
    then(function(result){
        // handle success
        return result;
    }, function errorHandler1(error){
        // handle error, exactly as if this was a separate catch in the chain.

    }).catch(function errorHandler2(error){
        // handle errors from errorHandler1
    });

3) This is exactly where $q.reject can come in handy:

3)这就是q值。拒绝可以派上用场:

promise.
    catch(function(error){
       //Decide you can't handle the error
       return $q.reject(error); //This forwards the error to the next error handler;
    }).catch(function(error){
       // Here you may handle the error or reject it again.
       return 'An error occurred'; 
       //Now other errorFn in the promise chain won't be called, 
       // but the successFn calls will.
    }).catch(function(error){
       // This will never be called because the previous catch handles all errors.
    }).then(function(result){
       //This will always be called with either the result of promise if it was successful, or 
       //'An error occured' if it wasn't
    });

#2


5  

Ok this is my take from promises.

这是我的承诺。

  1. $q.reject(reason) returns a rejected promise with the reason passed as argument and defered. Reject rejects an existent defered whether its process has finished or not.

    $q.reject(原因)返回一个被拒绝的承诺,其理由作为论据通过并被推迟。拒绝拒绝一个存在的延迟,不管它的进程是否已经完成。

  2. errorFn gets launched when a promise is rejected and its argument is the reason why it was rejected. Catch is called when an error inside the promise process wasn't handled properly causeing the promise to raise and exception and not being either rejected or fulfilled .

    当一个承诺被拒绝时,errorFn就会启动,它的论点就是它被拒绝的原因。当承诺过程中的错误没有得到正确处理时,就会调用Catch,从而导致承诺提出和异常,而不会被拒绝或履行。

  3. You shoudn't have nested promises, you should have chained promises in which case the latest catch block should catch everything prior to it if no other block was specified to handle it.

    您不应该有嵌套的承诺,您应该有链式的承诺,在这种情况下,如果没有指定其他块来处理的话,最新的catch块应该在它之前捕获所有内容。

#1


89  

1) $q.reject() is a shortcut to create a deferred and then reject it immediately; I often use this in an errorFn if I can't handle the error.

1) $q.reject()是创建递延后立即拒绝的快捷方式;如果我处理不了这个错误,我经常会用这个。

2) Nothing, promise.catch(errorFn) is just syntactic sugar for promise.then(null, errorFn), just like the success and error methods of the $http service, so you can write code like the following:

没什么,承诺。catch(错误)只是语法上的承诺。然后(null, errorFn),就像$http服务的成功和错误方法一样,您可以编写如下代码:

promise.
    then(function(result){
        // handle success
        return result;
    }, function errorHandler1(error){
        // handle error, exactly as if this was a separate catch in the chain.

    }).catch(function errorHandler2(error){
        // handle errors from errorHandler1
    });

3) This is exactly where $q.reject can come in handy:

3)这就是q值。拒绝可以派上用场:

promise.
    catch(function(error){
       //Decide you can't handle the error
       return $q.reject(error); //This forwards the error to the next error handler;
    }).catch(function(error){
       // Here you may handle the error or reject it again.
       return 'An error occurred'; 
       //Now other errorFn in the promise chain won't be called, 
       // but the successFn calls will.
    }).catch(function(error){
       // This will never be called because the previous catch handles all errors.
    }).then(function(result){
       //This will always be called with either the result of promise if it was successful, or 
       //'An error occured' if it wasn't
    });

#2


5  

Ok this is my take from promises.

这是我的承诺。

  1. $q.reject(reason) returns a rejected promise with the reason passed as argument and defered. Reject rejects an existent defered whether its process has finished or not.

    $q.reject(原因)返回一个被拒绝的承诺,其理由作为论据通过并被推迟。拒绝拒绝一个存在的延迟,不管它的进程是否已经完成。

  2. errorFn gets launched when a promise is rejected and its argument is the reason why it was rejected. Catch is called when an error inside the promise process wasn't handled properly causeing the promise to raise and exception and not being either rejected or fulfilled .

    当一个承诺被拒绝时,errorFn就会启动,它的论点就是它被拒绝的原因。当承诺过程中的错误没有得到正确处理时,就会调用Catch,从而导致承诺提出和异常,而不会被拒绝或履行。

  3. You shoudn't have nested promises, you should have chained promises in which case the latest catch block should catch everything prior to it if no other block was specified to handle it.

    您不应该有嵌套的承诺,您应该有链式的承诺,在这种情况下,如果没有指定其他块来处理的话,最新的catch块应该在它之前捕获所有内容。