Angular 2: Convert Observable to Promise

后端 未结 7 686
耶瑟儿~
耶瑟儿~ 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:27

    you dont really need to do this just do ...

    import 'rxjs/add/operator/first';
    
    
    this.esQueryService.getDocuments$.first().subscribe(() => {
            event.enableButtonsCallback();
          },
          (err: any) => console.error(err)
        );
        this.getDocuments(query, false);
    

    first() ensures the subscribe block is only called once (after which it will be as if you never subscribed), exactly the same as a promises then()

提交回复
热议问题