TypeScript - wait for an observable/promise to finish, and return observable

后端 未结 3 1075
天命终不由人
天命终不由人 2020-12-09 01:59

I am quite new to TypeScript & RxJS, and I am trying to return an Observable after another Observable is finished:

public myObs         


        
3条回答
  •  半阙折子戏
    2020-12-09 02:11

    While flatMap() may work, since you are not passing in a parameter that is used[see param (x)], the best operator for you to use in this scenario is forkJoin().

    Please see this example: https://stackoverflow.com/a/38049268/1742393

       Observable.forkJoin(
        this.http.get('/app/books.json').map((res:Response) => res.json()),
        this.http.get('/app/movies.json').map((res:Response) => res.json())
    ).subscribe(
      data => {
        this.books = data[0]
        this.movies = data[1]
      },
      err => console.error(err)
    );
    

提交回复
热议问题