如果有效载荷长度<1,则返回不同的可观察量。 1

时间:2017-06-17 01:09:42

标签: javascript redux rxjs rxjs5 redux-observable

我有这个史诗:

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))
        )
    })

我想只返回

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

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

Attmept:

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))
        )
    })

收到错误:

  

您提供了“未定义”的预期流。你可以提供   一个Observable,Promise,Array或Iterable。

如何返回上面的Observable.of,如果showAlertDialog,则返回action.payload.products.length < 1的观察点。

1 个答案:

答案 0 :(得分:2)

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)
  ))
})