如何在此之前等待另一个承诺时返回承诺

时间:2015-06-18 19:01:46

标签: javascript promise bluebird

我的视图模型调用服务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”promise库。

1 个答案:

答案 0 :(得分:2)

then()已经产生了这个承诺。您需要做的只是return来自您的方法:

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