2017-06-29 96 views
0

我将数据从阵列中依次发送到服务器。在完成先前的请求之后,每个下一个调度应该被调用。我如何使用RxJS来实现它?如何实现步骤,通过使用RxJS一步异步处理?

P.S.它是有角度的4应用程序。

+1

使用'flatMap()' – CozyAzure

+1

你可以添加一些代码吗? 你尝试过什么哪出错 –

+0

这已经很好说明如下:https://stackoverflow.com/documentation/rxjs/8247/common-recipes/28035/sending-multiple-sequential-http-requests#t=201706291652523651924 – martin

回答

0

如果你想你的链条Observables,使用.flatMap()。这是一样.then()Promise

假设你有3个HTTP调用功能,在你的服务,称为firstRequest()secondRequest()thirdRequest()。你可以像这样链接它们

myService.firstRequest() 
    .flatMap(result1 => { 
     //do something with result1 from firstRequest 
     return myService.secondRequest(result1) 
      .flatMap(result2 => { 
       //do something with result2 from secondRequest 
       return myService.thirdRequest(result2); 
      }); 
    }) 
    .subscribe(result3 => { 
     //finally do something with your result3 that is from thirdRequest 
    });