[英]What is the difference between these two syntax
If i have如果我有
promise = userService.updateUser($stateParams.userId, req);
promise.then(
function(user) {
logger.logSuccess('Updated user');
$scope.resetForm();
WizardHandler.wizard().goTo(0);
return user;
},
function(error) {
logger.logError('Ups an error has occurred');
console.error('error updating user: ' + error);
}
);
promise.then(function(user) {
_.each(uploader.getNotUploadedItems(), function(item) {
return item.formData.push({
id: user.id
});
});
});
Then if the updateUser fails the log will be shown and then second then
will not be executed however if i have然后,如果UpdateUser两个失败日志将被示出和然后第二then
将不被然而,如果我有执行
promise = userService.updateUser($stateParams.userId, req).then(
function(user) {
logger.logSuccess('Updated user');
$scope.resetForm();
WizardHandler.wizard().goTo(0);
return user;
},
function(error) {
logger.logError('Ups an error has occurred');
console.error('error updating user: ' + error);
}
);
promise.then(function(user) {
_.each(uploader.getNotUploadedItems(), function(item) {
return item.formData.push({
id: user.id
});
});
});
The second then will be executed然后将执行第二个
I can't figure out why, i mean isn't this just regular chaining ?我不知道为什么,我的意思是这不只是常规链接吗?
if the updateUser fails the log will be shown and then second then will not be executed如果 updateUser 失败,将显示日志,然后第二个将不会执行
Yes, because you're branching:是的,因为你正在分支:
success: - logSuccessAndResetForm()
| - makeNewFormData()
updateUser() --+
promise |
error - logError()
but when using regular chaining the second chain will be executed但是当使用常规链接时,将执行第二个链
Yes, of course.是的当然。 Your error handler handles the error and the promise
is fulfilled with the return value.您的错误处理程序处理错误,并通过返回值实现promise
。
success: - logSuccessAndResetForm() success: - makeNewFormData()
| \ |
updateUser() --+ >- promise --+
| / |
error - logError() ´ error: (nothing)
See also this answer for prettier control flow diagrams of similar code.另请参阅此答案以获取类似代码的更漂亮的控制流程图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.