發送 forkjoin api,每個 api 之間有延遲 (Send forkjoin api with a delay in between each api)


問題描述

發送 forkjoin api,每個 api 之間有延遲 (Send forkjoin api with a delay in between each api)

我目前正在處理一個 Angular 項目,我必須使用 forkjoin 以 ngrx 效果發送多個 api。這有一個問題,我必須延遲發送多個 api,就好像我一次發送一樣,它們被取消了。我的調用效果如下所示:

@Effect()
  timeslot$ = this.actions$
    .ofType(a.ItemActionTypes.GET_TIME_SLOTS)
    .map((action: a.GetTimeSlots) => action.payload)
    .switchMap(payload => {
      const serviceCol = payload.map(pl => this.service.getTimeSlots(pl));

       return Observable.forkJoin(serviceCol).pipe(
        mergeMap(result => {
          return [
            new a.GetTimeSlotsSuccess(
              ItemSearchAdaptor.allTimeSlotAdaptor(result)
            )];
        })
      ).catch((e: HttpErrorResponse) => {
        return Observable.of({});
      });
    });

誰能建議我可以使用任何方法來延遲 API 發送之間的延遲。


參考解法

方法 1:

getTimeSlots() method should return the observables with delay as shown give below. delay operator can be used to delay the request.

Be aware that if any of the inner observables supplied to forkJoin error you will lose the value of any other observables that would or have already completed if you do not catch the error correctly on the inner observable. If you are only concerned with all inner observables completing successfully you can catch the error on the outside.

getTimeSlots(pl){
            let observables = [];

            observables.push(http.doDelete(null, {})
                .pipe(delay(1000),
                    catchError(error => of(error))
            ))

            retur observables;
}

(by VithushanSudhakar)

參考文件

  1. Send forkjoin api with a delay in between each api (CC BY‑SA 2.5/3.0/4.0)

#ngrx-effects #Angular #angular6 #fork-join






相關問題

rxjs減少不繼續 (rxjs reduce does not continue)

從 reducer/action 觸發 API 重新加載的正確方法是什麼? (What's the proper way to trigger the API reload from reducer/action?)

Angular NgRx 效果,如何傳遞參數? (Angular NgRx effects, how to pass a parameter?)

NgRx 效果無限循環 (NgRx Effects infinite loop)

發送 forkjoin api,每個 api 之間有延遲 (Send forkjoin api with a delay in between each api)

如何從所有待處理的請求中取消特定請求 (How to cancel specific request from all pending request)

使用 NgRx 存儲優化 Angular 內存以避免內存洩漏 (Angular memory optimization with NgRx store to avoid memory leaks)

NgRx EffectsModule 導入組 (NgRx EffectsModule imports group)

切換地圖後在NGRX特效點擊功能中獲取動作道具 (Get action props in the NGRX effects tap function after switch Map)

如何防止在 NgRx 中第二次加載數據? (How prevent loading data second time in NgRx?)

需要幫助了解這種 Ngrx 效果 (Need help understanding this Ngrx effect)

測試效果 NgRx Angular 11 (Testing effects NgRx Angular 11)







留言討論