什么';当处理来自AngularJS的错误时,使用`.catch(function(error)`和`.function(errResponse)`有什么区别?
我正在阅读AngularJS的启动和运行。在第6章中,它提到了处理承诺中的错误,如:什么';当处理来自AngularJS的错误时,使用`.catch(function(error)`和`.function(errResponse)`有什么区别?,angularjs,angular-promise,Angularjs,Angular Promise,我正在阅读AngularJS的启动和运行。在第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/' + userRespons
$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()
(例如,这里的答案是:使用.catch()
,如下所示:
BaseService.fetch.stuffs
.then(function(data) {
self.stuffies = data;
console.log(self.stuffies);
}).catch(function(errorResponse) {
self.cerrorMessages = errorResponse.data;
});
我的问题是,上述方法与书中所示的方法有什么区别:
BaseService.fetch.stuffs
.then(function(data) {
self.stuffies = data;
console.log(self.stuffies);
}, function(error) {
self.cerrorMessages = errorResponse.data;
});
首选什么?根据
catch(errorCallback)
–promise的简写。然后(null,errorCallback)
这意味着您可以互换使用这两种语法。区别:
如果服务api中有一些错误,那么函数(错误){}
将捕获它
但是如果您的成功方法函数(数据){}
抛出一些错误,那么只有.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
});
首选方案:
promise().then(function (data) {
// handle data
}, function (error) {
// handle error from api
}).catch(function (err) {
// handle error from response.
});
catch语法更好,因为它将错误处理分离到自己的单个通道,远离控制流,这被认为是一种良好的实践,由bluebird.js和其他人推广,而使用错误处理函数作为then函数的第二个参数可以被认为是一种反pat燕鸥 你可以找到一些关于它们的非常好的读物: 通过一个例子可以说明两者之间的不同。对于您发布的第一段代码:
$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
});
如果不只是返回承诺,而是在第一个承诺解析程序中调用另一个异步函数,则此函数中的错误将永远不会到达最后一个错误处理程序,如果我忘记单独处理来自该新函数的错误,它将被吞没
但是,此问题可以通过以下方法解决:
$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
});
你确定它是这样工作的吗?你试过了吗?我不知道嵌套函数的错误怎么会出现在处理程序中。事实上,我认为你的两个示例是相同的,只是使用了稍微不同的语法。这不是OP所要求的。它基本上是1,然后是2。这个问题肯定是重复的,因为它是d讨论了这么多次。