在一个史诗RXJS中调用另一个史诗

时间:2020-04-18 19:56:44

标签: javascript react-redux rxjs rxjs-observables

我一般对RXJS和开发都是陌生的。我最近开始使用rxjs,发现自己陷入了以下问题,请提供帮助/指导和一些解释。

    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)),
    );

我的问题是,在我的第一个史诗调用api并成功请求之后,我将如何调用我的第二个史诗。想要在updateSomethingSuccess操作之后在第一个史诗中调用,这会向商店添加响应,然后再调用第二个api。

1 个答案:

答案 0 :(得分:0)

只需返回一个动作即可。 样本中的代码

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

使用

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

其中 updateSomethingSuccess(response)是类似以下内容的动作创建者

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

然后将执行具有类型UPDATE_SOMETHING2的史诗2。

简直太简单了。您必须根据需要进行修改

相关问题