在处理AngularJS承诺中的错误时,使用`.catch(function(error)`和`function(errResponse)`之间的区别是什么?

时间:2017-04-01 04:55:51

标签: angularjs angular-promise

我正在阅读AngularJS Up and Running。在第6章中,它提到了像这样的承诺中的错误:

$http.get('/api/server-config').then(function(configResponse) {
    return $http.get('/api/' + configResponse.data.USER_END_POINT);
}).then(function(userResponse) {
    return $http.get('/api/' + userResponse.data.id + '/items');
}).then(function(itemResponse) {
    // Display items here
}, function(error) {
    // Common error handling
});

在其他地方,我看到.catch()正在使用(例如,答案在这里:Assigning variable from a factory to a control doesn't work使用.catch(),如下所示:

BaseService.fetch.stuffs
.then(function(data) {
    self.stuffies = data;
    console.log(self.stuffies);
}).catch(function(errorResponse) {
    self.cerrorMessages = errorResponse.data;
});

我的问题是,上面的menthod和书中显示的方法有什么区别:

BaseService.fetch.stuffs
.then(function(data) {
    self.stuffies = data;
    console.log(self.stuffies);
}, function(error) {
    self.cerrorMessages = errorResponse.data;
});

什么是首选?

3 个答案:

答案 0 :(得分:0)

根据AngularJS 1.2 documentation

catch(errorCallback) - promise.then(null, errorCallback)

的简写

这意味着您可以互换使用2种语法。

答案 1 :(得分:0)

差异:

如果服务api出现错误,则function(error) {}会抓住它。

但是如果你的成功方法function(data){}引发了一些错误,那么只有.catch()可以抓住它。

示例:

promise().then(function (data) {
  throw new Error('execption from success');
}).catch(function (err) {
  // execption from success caught!!
});

promise().then(function (data) {
  throw new Error('execption from success');
}, function (error) {
  // execption from success : NOT caught
});

Read more

首选:

promise().then(function (data) {
 // handle data
}, function (error) {
  // handle error from api
}).catch(function (err) {
  // handle error from response.
});

答案 2 :(得分:0)

catch 语法更好,因为它将错误处理与其自身和单个通道分开,远离控制流,它被认为是良好的做法,由bluebird.js和其他人推广,而使用错误处理函数作为then函数的第二个参数可以视为反模式。

你可以找到一些非常好的读物:

http://www.datchley.name/promise-patterns-anti-patterns/

http://taoofcode.net/promise-anti-patterns/

https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns

两者之间的差异可以通过一个例子来说明。至于您发布的第一段代码:

$http.get('/api/server-config').then(function(configResponse) {
    // create another promising chain here
    $http.get("/something-else").then(function(response) {
       // Do something
    }); 
    return $http.get('/api/' + configResponse.data.USER_END_POINT);
}).then(function(userResponse) {
    return $http.get('/api/' + userResponse.data.id + '/items');
}).then(function(itemResponse) {
    // Display items here
}, function(error) {
    // Common error handling
});

如果不是只返回promise,我会在第一个promise解析器中调用另一个异步函数,此函数的错误将永远不会到达最后一个错误处理程序,如果我忘记单独处理来自该新函数的错误,它将被吞下去。

但是,可以使用catch来解决此问题:

$http.get('/api/server-config').then(function(configResponse) {
    // create another promising chain here
    $http.get("/something-else").then(function(response) {
       // Do something
    }); 
    return $http.get('/api/' + configResponse.data.USER_END_POINT);
}).then(function(userResponse) {
    return $http.get('/api/' + userResponse.data.id + '/items');
}).then(function(itemResponse) {
    // Display items here
}).catch(function(error) {
    // error from nested function something else, can also be handled here 
    // within a single error handling channel
});