I have try and catch once that is resolved my code is not reaching to last return Promise.resolve(returnValue)
and throwing empty {} object , Any help will be appreciated to resolve below issue.
main.ts
private async combineData(data: any, request: any): Promise < any > {
const details: any = [];
const indexIDs = this.indexIDs;
delete data.header.IndexOfRequestThatFailed;
const returnValue = {
"header": {
"statusDesc": "",
"statusCode": "",
"refID": ""
},
"details": details
}
as IGetPaymentHistoryResponse;
const paymentHistoryDetails = {
header: returnValue.header,
details: returnValue.details
};
// saving details to cache
const saveDetails = {}
as ICacheRequest;
saveDetails.cacheobject = paymentHistoryDetails;
saveDetails.cachetype = "CombinedPaymentHistoryCache";
saveDetails.key =
request.body.getPaymentHistoryRequest.header.serviceContext.tokenID;
const _passedParams: ILogParams = {}
as ILogParams;
_passedParams.refId =
request.body.getPaymentHistoryRequest.header.serviceContext.refID;
_passedParams.sessionId =
request.body.getPaymentHistoryRequest.header.serviceContext.tokenID;
_passedParams.opname = "getPaymentHistory";
_passedParams.starttime = moment().toString();
_passedParams.endtime = "";
try {
await CacheController.getInstance().saveDetailsWrapper(saveDetails,
_passedParams);
return Promise.resolve({
getPaymentHistoryResponse: paymentHistoryDetails
});
} catch (err) {
return Promise.reject({
getPaymentHistoryResponse: err
});
}
return Promise.resolve(returnValue);
}
Use simple return
statement as async
function returns Promise by default. So your returned value is equal to resolving a value, and throwing is rejection
try {
await CacheController.getInstance().saveDetailsWrapper(saveDetails,
_passedParams);
return {
getPaymentHistoryResponse: paymentHistoryDetails
};
} catch (err) {
throw {
getPaymentHistoryResponse: err
};
}
Code will not reach last line because there is a return statement in try and catch.
It is like you write
if(check) {... return 1}
else {return 2}
return 3
And ask why 3 is never returned
Update
I understood :) So async/await gives you ability to write asynchronous code in a sync way. If you want to make a request without blocking other code then just use plain then
able syntax:
private combineData(data: any, request: any): any {
const details: any = [];
const indexIDs = this.indexIDs;
delete data.header.IndexOfRequestThatFailed;
const returnValue = {
"header": {
"statusDesc": "",
"statusCode": "",
"refID": ""
},
"details": details
}
as IGetPaymentHistoryResponse;
const paymentHistoryDetails = {
header: returnValue.header,
details: returnValue.details
};
// saving details to cache
const saveDetails = {}
as ICacheRequest;
saveDetails.cacheobject = paymentHistoryDetails;
saveDetails.cachetype = "CombinedPaymentHistoryCache";
saveDetails.key =
request.body.getPaymentHistoryRequest.header.serviceContext.tokenID;
const _passedParams: ILogParams = {}
as ILogParams;
_passedParams.refId =
request.body.getPaymentHistoryRequest.header.serviceContext.refID;
_passedParams.sessionId =
request.body.getPaymentHistoryRequest.header.serviceContext.tokenID;
_passedParams.opname = "getPaymentHistory";
_passedParams.starttime = moment().toString();
_passedParams.endtime = "";
// do not return here to return on the last line
CacheController.getInstance().saveDetailsWrapper(saveDetails, _passedParams).then(response => ..., err => ...);
return returnValue;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.