How to 'wait' for two observables in RxJS

前端 未结 7 1521
温柔的废话
温柔的废话 2020-12-13 12:14

In my app i have something like:

this._personService.getName(id)
      .concat(this._documentService.getDocument())
      .subscribe((response) => {
              


        
相关标签:
7条回答
  • 2020-12-13 12:42

    Have a look at the 'combineLatest' method, it might be appropriate here. http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-combineLatest

    const { Observable } = Rx
    
    const name$ = this._personService.getName(id);
    const document$ = this._documentService.getDocument();
    
    Observable
        .combineLatest(name$, document$, (name, document) => ({ name, document }))
        .first() // or not, implementation detail
        .subscribe(({ name, document }) => {
            // here we have both name and document
            this.showForm()
        })
    
    0 讨论(0)
提交回复
热议问题