How to wait for first Observable to finish before executing others in parallel using RxJS

人走茶凉 提交于 2019-12-23 16:03:12

问题


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:


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
      });
   }
}


来源:https://stackoverflow.com/questions/40164578/how-to-wait-for-first-observable-to-finish-before-executing-others-in-parallel-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!