如何使用异步/等待功能解决Promise?

时间:2019-01-15 18:46:20

标签: javascript typescript asynchronous async-await

我已尝试捕获,一旦解决,我的代码就没有到达最后的返回Promise.resolve(returnValue)并抛出空的{}对象,将感谢您提供任何帮助来解决以下问题。

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);

}

1 个答案:

答案 0 :(得分:1)

使用简单的return语句作为async函数默认情况下会返回Promise。因此,您返回的值等于解析一个值,而抛出就是拒绝

try {
        await CacheController.getInstance().saveDetailsWrapper(saveDetails,
            _passedParams);
        return {
            getPaymentHistoryResponse: paymentHistoryDetails
        };
    } catch (err) {
        throw {
            getPaymentHistoryResponse: err
        };
    }

代码不会到达最后一行,因为try and catch中有一个return语句。

就像你写

if(check) {... return 1}
else {return 2}
return 3

问为什么从不返回3

更新

我了解:)因此async / await使您能够以同步方式编写异步代码。如果您要发出请求而不阻塞其他代码,则只需使用简单的then可用语法即可:

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;

}