Angular Promises and Chaining:如果链存在业务数据错误,如何打破链 [英] Angular Promises and Chaining: How to break a chain if it has business data errors
问题描述
我以为我这些都已经在以前的项目中找到了所有这些......显然不是。
目标:接受调用其他服务的服务,如果有的话返回任何类型的错误(不是200 的状态)然后我需要异步的东西等待而不是继续。
在我看来,我从来没有看到过那么好的例子,因为它非常简单。
我读了很多关于Angular(1)在幕后做什么的文章,我看到有$ q,.then,.success等......
似乎我遇到了返回
的问题,并且在没有检查问题的情况下进行了其他嵌套和捆绑服务调用。
基本上这张图片显示了回来的内容
数据:null(不好)
errorList数组
这是我的操作顺序
this.submitEnrollment = function(enrollment){
return getSubmit(requestData);
}
//
以下的哪个CALL变量getSubmit = function(request){
return SparkRequestService
.submitRequest(request)
.then(
function(resData){
console.log(resData,resData);
enrollmentService.resetEnrollment();
return resData;
},
函数(resData){
console.log('error');
}
);
}
然后我肯定在调用 SparkRequestService.submitRequest(请求)
但是基于附加的图片,我收到 resData
$中的错误b $ b
所以,似乎我需要查询 resData
吧?那么我真的不应该允许这个其他服务被称为 enrollmentService.resetEnrollment();
我怎么能重构停止处理? if语句在 .then
中?
为防止拒绝处理程序转换被拒绝的承诺履行承诺,重要的是使用拒绝处理程序中的 throw
语句:
var getSubmit = function(请求){
返回SparkRequestService
.submitRequest(请求)
.then(
function(resData){
console.log(resData,resData);
enrollmentService.resetEnrollment();
返回resData;
},
函数(errorResponse){
console.log('error');
//重要
//投掷链拒绝
抛出errorResponse;
}
);
}
当函数省略 return $ c时$ c>或
抛出
语句,该函数返回值 undefined
。这将转换被拒绝的承诺,以履行已解决的价值 undefined
。
问题是......这是一个包含在退货对象中的业务错误
要转换对已拒绝承诺的履行承诺,请使用 throw
语句。
this.submitEnrollment = function(enrollment){
var promise = getSubmit(requestData);
var newPromise = promise.then(function(response){
if(response.data.hasErrors){
console.log(response.data.errorList);
response。 data.errorList.push(submitEnrollent:Rejected);
// THROW创建拒绝
抛出响应;
} else {
// RETURN响应链成功
返回响应;
}
});
返回newPromise;
}
当承诺被转换拒绝时,将跳过链中的所有后续成功处理程序。将跟踪链,直到找到拒绝处理程序。
I thought that I had this all figured out on previous projects through the years.. Apparently not.
Goal : Take Service that calls other Services and if there is any type of error being returned ( not a status of 200 ) then I need the async thing to be waiting and not proceeding.
Seems to me like I don't ever see really that great of examples as it is all very simplistic.
I read various articles about what Angular (1) is doing under the hood , and i see that there are $q, .then, .success etc..
Seems that I am having issues with return
and with other nested and bundled service calls being made without any checking of a problem.
Essentially this image shows what is coming back
data : null ( that is bad) errorList Array1 0 "This order cannot be submitted... " ( bad too) hasErrors : true ( bad as well)
So that data is important to me to capture and display to user and then NOT move onto more processing
This is my order of operations
this.submitEnrollment = function (enrollment) {
return getSubmit(requestData);
}
// Which CALLS below
var getSubmit = function (request) {
return SparkRequestService
.submitRequest(request)
.then(
function (resData) {
console.log("resData", resData);
enrollmentService.resetEnrollment();
return resData;
},
function (resData) {
console.log('error');
}
);
}
Then I'm certainly calling SparkRequestService.submitRequest(request)
but based on the image attached, I am getting the error in the resData
So, it seems that I need to interrogate the resData
right? So then I really should NOT ALLOW this other service to be called enrollmentService.resetEnrollment();
How can i refactor to stop from that getting processed? if statement in the .then
?
To prevent a rejection handler from converting a rejected promise to a fulfilled promise it is important use a throw
statement in the rejection handler:
var getSubmit = function (request) {
return SparkRequestService
.submitRequest(request)
.then(
function (resData) {
console.log("resData", resData);
enrollmentService.resetEnrollment();
return resData;
},
function (errorResponse) {
console.log('error');
//IMPORTANT
//throw to chain rejection
throw errorResponse;
}
);
}
When a function omits a return
or throw
statement, the function returns a value of undefined
. This will convert a rejected promise to a fulfilled promise that resolves with a value of undefined
.
Problem is that ... it is a business error wrapped up in a return object
To convert a fulfilled promise to a rejected promise, use a throw
statement.
this.submitEnrollment = function (enrollment) {
var promise = getSubmit(requestData);
var newPromise = promise.then(function(response) {
if (response.data.hasErrors) {
console.log(response.data.errorList);
response.data.errorList.push("submitEnrollent: Rejected");
//THROW to create rejection
throw response;
} else {
//RETURN response to chain success
return response;
}
});
return newPromise;
}
When a promise is converted to a rejection, all subsequent success handlers in the chain will be skipped. The chain will be followed until a rejection handler is found.
这篇关于Angular Promises and Chaining:如果链存在业务数据错误,如何打破链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!