2017-06-29 102 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 
    });