Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dzNavitski/92df0d2bfb6ab292a2af8b36822b1d51 to your computer and use it in GitHub Desktop.
Save dzNavitski/92df0d2bfb6ab292a2af8b36822b1d51 to your computer and use it in GitHub Desktop.
There is no requirement that you make a one-to-one in/out ratio. So you can emit multiple actions using flatMap if you need to:
const loaded = (results) => ({type: 'RESULTS_LOADED', results});
const otherAction = (results) => ({type: 'MY_OTHER_ACTION', results});
searchEpic = (action$) =>
action$
.ofType('SEARCH')
.mergeMap(
Observable
.fromPromise(searchPromise)
// Flattens this into two events on every search
.flatMap((data) => ([loaded(results), otherAction(results)]))
)
Note that any Rx operator that accepts an Observable also can accept a Promise, Array, or Iterable; consuming them as-if they were streams. So we could use Observable.of instead for the same effect:
.flatMap((data) => Observable.of(loaded(results), otherAction(results)))
Which one you use depends on your personal style preferences.
@linonetwo
Copy link

linonetwo commented Jan 31, 2018

But it can't do something like

function* StartUpSaga(): Generator<IOEffect, void, any> {
  yield put(loadConfigAction.trigger());
  yield take(loadConfigAction.SUCCESS);
  yield put(loadCardFromFsAction.trigger());
}

in redux saga.

In redux-observable, you can only dispatch action at the end.

@linonetwo
Copy link

Ok, my fault. I ignored the fact that it is a stream.
We can do it like this: redux-observable/redux-observable#62 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment