Last active
April 2, 2019 04:02
-
-
Save dzNavitski/92df0d2bfb6ab292a2af8b36822b1d51 to your computer and use it in GitHub Desktop.
redux-observable - dispatch multiple redux actions in a single epic (http://stackoverflow.com/questions/40886655/redux-observable-dispatch-multiple-redux-actions-in-a-single-epic)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
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
But it can't do something like
in redux saga.
In redux-observable, you can only dispatch action at the end.