Return 如果有效负载长度 < 1,则可观察到不同

Return different observable if payload length is < 1

我有这部史诗:

export const fetchProductsFulfilledEpic = action$ =>
  action$.ofType(FETCH_PRODUCTS_FULFILLED)
    .mergeMap(action => {
      return Observable.of(
        updateSearchResults(action.payload),
        toggleMenu(),
        updateRegion(action.payload)
      ).catch(error => Observable.of(
        fetchProductsRejected(error))
        )
    })

我只想return

updateSearchResults(action.payload),
toggleMenu(),
updateRegion(action.payload)

如果 action.payload.products.length 大于 0,否则显示警报。

尝试:

export const fetchProductsFulfilledEpic = action$ =>
  action$.ofType(FETCH_PRODUCTS_FULFILLED)
    .mergeMap(action => {
      Observable.if(() => action.payload.products.length > 0,
        Observable.of(
          updateSearchResults(action.payload),
          toggleMenu(),
          updateRegion(action.payload)
        ), Observable.of(...enter alert observable here...)
      ).catch(error => Observable.of(
        fetchProductsRejected(error))
        )
    })

获取错误:

You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

我如何 return 上面的 Observable.of 和 return 一个可观察到 showAlertDialog if action.payload.products.length < 1

mergeMap 中的正常条件如何?

export const fetchProductsFulfilledEpic = action$ =>
  action$.ofType(FETCH_PRODUCTS_FULFILLED)
    .mergeMap(action => {
      if (action.payload.products.length > 0) {
        return Observable.of(
          updateSearchResults(action.payload),
          toggleMenu(),
          updateRegion(action.payload)
        );
      } else {
        return Observable.of(
          showAlertDialog()
        );
      }
    })

附带一点,您的 catch 目前是无关紧要的,因为 Observable.of 永远不会抛出错误:

.mergeMap(action => {
  return Observable.of(
    updateSearchResults(action.payload),
    toggleMenu(),
    updateRegion(action.payload)
  // this is catching errors on the above Observable but
  // this Observable will never ever throw errors
  ).catch(error => Observable.of(
    fetchProductsRejected(error)
  ))
})