问)如何将以下 observable 转换为 promise,以便可以使用 .then(...)
调用它?
我想转换为承诺的方法:
this._APIService.getAssetTypes().subscribe(
assettypes => {
this._LocalStorageService.setAssetTypes(assettypes);
},
err => {
this._LogService.error(JSON.stringify(err))
},
() => {}
);
它调用的服务方法:
getAssetTypes() {
var method = "assettype";
var url = this.apiBaseUrl + method;
return this._http.get(url, {})
.map(res => <AssetType[]>res.json())
.map((assettypes) => {
assettypes.forEach((assettypes) => {
// do anything here you might need....
});
return assettypes;
});
}
谢谢!
原文由 Dave 发布,翻译遵循 CC BY-SA 4.0 许可协议
rxjs7
https://indepth.dev/posts/1287/rxjs-heads-up-topromise-is-being-deprecated
rxjs6
https://github.com/ReactiveX/rxjs/issues/2868#issuecomment-360633707
…
this._APIService.getAssetTypes() .map(assettypes => { this._LocalStorageService.setAssetTypes(assettypes); }) .toPromise() .catch(err => { this._LogService.error(JSON.stringify(err)); });
”`