【发布时间】:2016-08-15 02:35:56
【问题描述】:
Q) 如何将以下 observable 转换为 Promise,以便我可以使用 .then(...) 调用它?
我想转换成promise的方法:
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;
});
}
谢谢!
【问题讨论】:
-
下面的大多数答案都有
toPromise()运算符,在 RxJS 5.5+ 中已被弃用
标签: typescript angular promise observable