Redux Saga async/await pattern

后端 未结 3 1137
伪装坚强ぢ
伪装坚强ぢ 2021-02-01 13:14

I\'m using async/await throughout my codebase. Because of this my api calls are defined by async functions

async function apiFetchFoo {
  return await apiCall(..         


        
3条回答
  •  误落风尘
    2021-02-01 13:41

    Yes, that's the standard way to use Redux-Saga.

    You should never be calling the await function directly inside the saga-generator, because redux-saga is for orchestrating the side-effects. Therefore, any time that you want to run a side-effect you should do it by yielding the side-effect through a redux-saga effect (usually: call or fork). If you do it directly without yielding it through a redux-saga effect, then redux-saga won't be able to orchestrate the side-effect.

    If you think about it, the redux-saga generator is completely testable without the need of mocking anything. Also, it helps to keep things decoupled: if your apiFetchFoo returned a promise, the saga would still work the same.

提交回复
热议问题