tomz
tomz

Reputation: 341

How to emit two actions for ajax response in redux observable epic

I have redux observable epic that maps AJAX response to single action ("createSuccess"):

    export const create = (action$) => {
    return action$.pipe(
        ofType(CREATE_REQUEST),
        mergeMap(action => {
            return ajax({...}
            ).pipe(
                map((response) => createSuccess(response.response)),
                catchError(error => of(createFailure(error)))
            );
        })
    );
};

How do I fire different action (let's call it redirect) in addition to createSuccess? Also, is it possible to make that action conditional based on the response I'm getting from ajax call?

Upvotes: 1

Views: 400

Answers (1)

martin
martin

Reputation: 96891

Instead of map you can use mergeMap (concatMap will work as well) and return an array from its callback:

mergeMap((response) => [
  otherAction(),
  createSuccess(response.response),
]),

Inside the callback you can put any logic you want that makes any array you want. The array you return is automatically iterated by mergeMap and each item is reemitted as a separate emission.

Upvotes: 1

Related Questions