问:如何将以下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; }); }
谢谢!
import 'rxjs/add/operator/toPromise'; import 'rxjs/add/operator/map'; ... this._APIService.getAssetTypes() .map(assettypes => { this._LocalStorageService.setAssetTypes(assettypes); }) .toPromise() .catch(err => { this._LogService.error(JSON.stringify(err)); });