在可通过Redux观察到的AJAX史诗中结合rxjs takeUntil和catchError

问题描述 投票:0回答:1

我试图在以下redux-observable史诗中“取消”( takeUntil )AJAX调用,同时保留catchError效果。

const someEpic = action$ =>
  action$.pipe(
    ofType(REQUEST_MAPPED_ESTABLISHMENTS),
    map( action => ( // transform data to url )),
    mergeMap( url  =>
      ajax({ url }).pipe(
        mergeMap( establishments => {
          const { error, data } = establishments.body
          if (error) throw 'some error';
          return of(actions.receiveMappedEstablishments(data))
        }),
        catchError(error =>
          (of(actions.requestMappedEstablishmentsFailure(error)))
        ),
      )
    ),
  )

上面的摘录如何重组为包含takeUntil ,例如以下内容?

takeUntil(action$.ofType(UI_HAS_CHANGED_SO_NEW_DATA_NOW_REQUIRED)) 

到目前为止,我仅成功完全取消了一些someEpic ,例如,将takeUntil插入到action$.pipe

rxjs redux-observable
1个回答
0
投票
const someEpic = action$ =>
  action$.pipe(
    ofType(REQUEST_MAPPED_ESTABLISHMENTS),
    map( action => ( // transform data to url )),
    mergeMap( url  =>
      ajax({ url }).pipe(
        mergeMap( establishments => {
          const { error, data } = establishments.body
          if (error) throw 'some error';
          return of(actions.receiveMappedEstablishments(data))
        }),
        catchError(error =>
          (of(actions.requestMappedEstablishmentsFailure(error))),
        takeUntil(action$.ofType(UI_HAS_CHANGED_SO_NEW_DATA_NOW_REQUIRED)) 
        ),
      )
    ),
  )
© www.soinside.com 2019 - 2024. All rights reserved.