Angular Promises 和 Chaining:如果链有业务数据错误,如何中断链 [英] Angular Promises and Chaining: How to break a chain if it has business data errors
问题描述
我以为这些年来我已经在以前的项目中弄清楚了这一切......显然没有.
目标:获取调用其他服务的服务,如果返回任何类型的错误(不是 200 状态),那么我需要异步事情等待而不是继续.
在我看来,我从来没有见过这么棒的例子,因为它们都非常简单.
我阅读了各种关于 Angular (1) 在幕后做了什么的文章,我看到有 $q、.then、.success 等.
似乎我在 return
和其他嵌套和捆绑的服务调用方面遇到了问题,而没有对问题进行任何检查.
基本上这张图片显示的是什么回来了
data : null(不好)errorList Array
这是我的操作顺序
this.submitEnrollment = 函数(注册){返回 getSubmit(requestData);}//下面调用var getSubmit = 函数(请求){返回 SparkRequestService.submitRequest(请求).然后(函数(resData){console.log("resData", resData);注册服务.resetEnrollment();返回 resData;},函数(resData){console.log('错误');});}
那我当然是在调用 SparkRequestService.submitRequest(request)
但根据附加的图像,我在 resData
那么,看来我需要询问 resData
对吗?那么我真的不应该允许这个其他服务被称为 enrollmentService.resetEnrollment();
我如何重构以停止处理?.then
中的 if 语句?
为了防止拒绝处理程序将被拒绝的承诺转换为已履行的承诺,使用 throw
拒绝处理程序中的语句:
var getSubmit = 函数(请求){返回 SparkRequestService.submitRequest(请求).然后(函数(resData){console.log("resData", resData);注册服务.resetEnrollment();返回 resData;},功能(错误响应){console.log('错误');//重要的//抛出链式拒绝抛出错误响应;});}
当函数省略return
或throw
语句时,函数返回undefined
的值.这将转换一个被拒绝的承诺为一个已履行的承诺,该承诺以undefined
的值解析.
问题是......这是一个包含在返回对象中的业务错误
要将已履行的承诺转换为被拒绝的承诺,请使用 throw
语句.
this.submitEnrollment = 函数(注册){var promise = getSubmit(requestData);var newPromise = promise.then(function(response) {如果(response.data.hasErrors){console.log(response.data.errorList);response.data.errorList.push("submitEnrollent: Rejected");//THROW 创建拒绝抛出响应;} 别的 {//返回链成功的响应返回响应;}});返回新承诺;}
当承诺转换为拒绝时,链中的所有后续成功处理程序都将被跳过.将遵循该链,直到找到拒绝处理程序.
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 和 Chaining:如果链有业务数据错误,如何中断链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!