我的视图模型提供了对服务A的调用,并且服务A需要调用另一个服务B。B将返回服务A所需的某些值。但这似乎不起作用。

这是我的代码。

class BillingService {
rest: BaseRest;
baseUrl: string;
configurationService: ConfigurationService;
constructor() {
    this.configurationService = new ConfigurationService();
    this.rest = new BaseRest({ basePath: this.baseUrl, isExternal: true });
}
getClaimsSummary() {
    this.configurationService.getBillingConfiguration().then((data: BillingConfigurationModel) => {
        this.baseUrl = data.billingBaseUrl;
        return this.rest.GET<ClaimSummaryModel>("claims/GetClaimsHistory", {});
    });
}}


视图模型正在调用getClaimsSummary

     this.billingService.getClaimsSummary().then((data: ClaimSummaryModel) => {
        //push to array
    });


getClaimsSummary取决于configurationService.getBillingConfiguration()返回的值(baseUrl)。我试图了解如何返回getClaimsSummary,以便它被viewmodel接受为承诺。

请注意,其余部分正在使用“ bluebird”承诺库。

最佳答案

then()已经兑现了这一承诺。您要做的就是从您的方法中return进行操作:

getClaimsSummary() {
    return this.configurationService.getBillingConfiguration().then((data: BillingConfigurationModel) => {
//  ^^^^^^
        this.baseUrl = data.billingBaseUrl;
        return this.rest.GET<ClaimSummaryModel>("claims/GetClaimsHistory", {});
    });
}

关于javascript - 在等待之前的另一个 promise 时如何返回 promise ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30923664/

10-11 10:32
查看更多