Angular 2: Convert Observable to Promise

后端 未结 7 692
耶瑟儿~
耶瑟儿~ 2020-11-27 18:00

Q) How do I convert the following observable to a promise so I can call it with .then(...)?

My method I want to convert to a promise:

7条回答
  •  伪装坚强ぢ
    2020-11-27 18:11

    rxjs6

    https://github.com/ReactiveX/rxjs/issues/2868#issuecomment-360633707

    Don't pipe. It's on the Observable object by default.

    Observable.of('foo').toPromise(); // this
    

    rxjs5

    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));
    });
    

提交回复
热议问题