【发布时间】:2021-08-25 10:17:00
【问题描述】:
我已经被 Promise 缠住了。我想等到 checkNewVersion() 完成,然后执行 then 语句,但此刻,'finished' 被首先调用,然后是 'api response'
page.ts
public ionViewDidLoad() {
this.platform.ready()
.then(() => {
this.versionChecker.checkNewVersion()
.then(response => {
console.log('finished')
})
});
}
版本检查器.ts
public checkNewVersion(force?: boolean): Promise<CheckResult> {
let platform = this.platform.is("ios") ? 'ios' : 'android';
if (force || this.shouldCheck()) {
return this.checkPromise = this.api.post('check-version', {platform: platform, version: '0.2.1'})
.then(response => {
console.log('api response')
let result = {
latest: response.latest,
}
return result;
});
}
return this.checkPromise;
}
api-service.ts
public post(path: string, body: any): Promise<any> {
//add location if we have it.
let postBody = {...body};
postBody.locationInfo = this.locationService.getUpdatedLocation();
return this.http.post(this.settings.api + path, postBody, { headers: this.headers })
.map((res: Response) => { return res.json(); })
.catch((err: Response, obs: Observable<any>) => {
return this.handleError(err, obs);
})
.toPromise();
}
【问题讨论】:
标签: javascript angular typescript es6-promise