How to wait for first Observable to finish before

2019-07-04 00:06发布

At the moment I have all 3 requests running in parallel. Now I need to wait for the first one to finish before I fire the other 2 in parallel. This is what I have at the moment:

return Observable
   .forkJoin(request1, request2, request3)
   .map((successValues: boolean[]) => {
      return successValues.every(x => x);
   })
   .do(success => {
      if (success) {
         this.store.dispatch({
            type: actions.UPDATE_SUCCESS
      });
   }
});

1条回答
Juvenile、少年°
2楼-- · 2019-07-04 00:50

You should use the switchMap operator.

request1.switchMap(response1 => {
    Observable
   .forkJoin(request2, request3)
   .map((successValues: boolean[]) => {
      return successValues.every(x => x);
   })
   .do(success => {
      if (success) {
         this.store.dispatch({
            type: actions.UPDATE_SUCCESS
      });
   }
}
查看更多
登录 后发表回答