I am using Redux-Observable Epic in a React & Redux project. I have multiple actions need to emit, originally this is what I have been doing,
1) Catch the START_ACTION in the Epic
2) Fetch remote data
3) Return a new Observanble
for example:
import fetch from 'isomorphic-fetch';
import {Observable} from 'rxjs/Observable';
import {switchMap} from 'rxjs/operator/switchMap';
import {mergeMap} from 'rxjs/operator/mergeMap';
const fetchAsync = (arg) => {
// return an observable of promise
return Observable::from(fetch(url + arg));
}
export function myEpic = (action$) => {
action$.ofType(START_ACTION)
::switchMap(action => {
return fetchAsync('/action.payload')
::mergeMap(result => {
return Observable::of({type: ACTION_COMPLETE, payload: result})
})
})
};
Now what if I have another action SECOND_ACTION
need to be emitted after the START_ACTION
and before the ACTION_COMPLETE
? In other word, without making sure the SECOND_ACTION
hits the reducer, the ACTION_COMPLETE
should not be emitted.
I could write another separate Epic function to do this, but is there any other easier way?
ACTION_COMPLETE
action while it waits for theSECOND_ACTION
? What if it never comes? – jayphelpsSECOND_ACTION
then immediately followed by theACTION_COMPLETE
? i.e. emit two actions – jayphelpsFIRST_ACTION
then immediately followed bySECOND_ACTION
. I am thinking about usingdispatch(SECOND_ACTION)
in the action function which returnFIRST_ACTION
– Jun QFIRST_ACTION
coming from? It's not in your question above. did you maybe meanACTION_COMPLETE
? – jayphelps