How to 'wait' for two observables in RxJS

前端 未结 7 1563
温柔的废话
温柔的废话 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:34

    Use forkJoin() method of observables. Check this link for reference

    From the RXJS docs

    This operator is best used when you have a group of observables and only care about the final emitted value of each. One common use case for this is if you wish to issue multiple requests on page load (or some other event) and only want to take action when a response has been receieved for all. In this way it is similar to how you might use Promise.all

    Observable.forkJoin([character, characterHomeworld]).subscribe(results => {
      // results[0] is our character
      // results[1] is our character homeworld
      results[0].homeworld = results[1];
      this.loadedCharacter = results[0];
    });
    

    Code taken from: https://coryrylan.com/blog/angular-multiple-http-requests-with-rxjs

提交回复
热议问题