typescript – Angular 2:将Observable转换为Promise
发布时间:2020-12-17 08:02:25 所属栏目:安全 来源:网络整理
导读:问:如何将以下observable转换为promise,以便我可以用.then(…)调用它? 我希望转换为承诺的方法: this._APIService.getAssetTypes().subscribe( assettypes = { this._LocalStorageService.setAssetTypes(assettypes); },err = { this._LogService.error(
问:如何将以下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)); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |