0

I'm quite new to RXJS and development in general. I started working with rxjs recently and I found myself stuck with the following issue and I would appreciate some help/ guidance and some explanation please.

    export const updateSomethingEpic1 = (action$) =>
 action$
    .ofType('UPDATE_SOMETHING')
    .switchMap(({ result }: { result }) =>
      //SOME API CALL
        .map(({ response }) => updateSomethingSuccess(response)) 

         **make call to second epic**

        .catch(err => updateSomethingError(err)),
    );


      //My second epic

        export const updateSomethingEpic2 = (action$) =>
 action$
    .ofType('UPDATE_SOMETHING2')
    .switchMap(({ result }: { result }) =>
      //SOME API CALL
        .map(({ response }) => updateSomethingSuccess2(response))
        .catch(err => updateSomethingError2(err)),
    );

My question is how would I make a call to my second epic after my first epic has called the api and made a successful request. Want to make a call in the first epic after updateSomethingSuccess action, which adds response to the store and then call the second api afterwards.

jayd
  • 11
  • 6

1 Answers1

0

Just do return an action. Code from your sample

.map(({ response }) => updateSomethingSuccess(response) **<--- make call to second epic**)  

use

.map(({ response }) => updateSomethingSuccess(response) )

where updateSomethingSuccess(response) is action creator like following

function updateSomethingSuccess(response) {
   return { type: 'UPDATE_SOMETHING2', payload: response}; // HERE
}

Then your epic 2 with ofType UPDATE_SOMETHING2 will be performed.

It's dead simple. You have to modify for your needs

Ivan Wong
  • 226
  • 3
  • 10
  • my `updateSomethingSuccess ` already returns something else, like `function updateSomethingSuccess(response) { return { type: 'UPDATED_SUCCESSFULY', payload: response}; }` – jayd Apr 18 '20 at 20:09
  • my updateSomethingSuccess already returns something else, like function updateSomethingSuccess(response) { return { type: 'UPDATED_SUCCESSFULY', payload: response}; }, so this example wouldn't work right? – jayd Apr 18 '20 at 22:28
  • @jayd Make sure you have second epic combined. Check function combineEpics() in your rootEpic. – Ivan Wong Apr 20 '20 at 05:28
  • In the rootEpic they are combined. But how would I trigger the other epic without changing the action creater of updateSomethingSuccess of the first epic – jayd Apr 20 '20 at 09:47