如何在使用RxJS并行执行其他Observable之前等待第一个Observable完成 [英] How to wait for first Observable to finish before executing others in parallel using RxJS

查看:330
本文介绍了如何在使用RxJS并行执行其他Observable之前等待第一个Observable完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我所有3个请求并行运行.现在,我需要等待第一个完成,然后再并行触发其他两个.这是我目前所拥有的:

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

推荐答案

您应使用switchMap运算符.

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

这篇关于如何在使用RxJS并行执行其他Observable之前等待第一个Observable完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆