2

I am really confused by redux saga. So whatever article I look at they explain that it is there to fix the side effect of redux and asynchronous calls and even they give explanation on the difference of saga and thunk but all of them are confusing. They lack the explanation of why I should use saga at all? what is wrong if I make the async call wait for it and then update the redux state.

What i need is a simple and clear explanation on why and in what occasion we need to use redux saga or thunk?

Am I right to say if I do not use saga then if I click 1000 times my code will run the async code and wait for result 1000 times but with saga I have a way to control over it either run all in parallel(fork) or just run the last one?

5
  • 1
    Does this answer your question? Why do we need middleware for async flow in Redux?
    – blaz
    Commented Nov 19, 2019 at 2:49
  • @blaz so for whatever action that needs to change the redux store (not the component state) and is asynchronous we better to use thunk or saga but we do not have to and if not using it still it is valid. right?
    – Learner
    Commented Nov 19, 2019 at 2:56
  • @blaz Also when we say redux is syncronous flow by nature still we can swait for the call and then proceed. So how is that different from saga or thunk?
    – Learner
    Commented Nov 19, 2019 at 2:59
  • @blaz Am I right to say if I do not use saga then if I click 1000 times my code will rin the asnc code and wait for result 1000 times but with saga I have a way to control over it either run all in parallel(fork) or just run the last one?
    – Learner
    Commented Nov 19, 2019 at 3:04
  • 1
    Please check my answer
    – blaz
    Commented Nov 19, 2019 at 4:41

1 Answer 1

0

so for whatever action that needs to change the redux store (not the component state) and is asynchronous we better to use thunk or saga but we do not have to and if not using it still it is valid. right?

First and foremost, redux-saga or redux-thunk are libraries built on top of Redux's middleware, which is used to handle concerns unrelated to the main data flow of redux, e.g. API calls, logging, routing. You can obviously write your own middlewares to handle async flow instead of using those libraries, which might lighten your app size a lot. However, redux-thunk and especially redux-saga have many syntax-sugar APIs to deal with complex usage of async flows, such as racing, sequencing API calls, cancelling API call based on condition and so on, which reduce a lot of working implementing equivalent logics by yourself.

Also when we say redux is syncronous flow by nature still we can swait for the call and then proceed. So how is that different from saga or thunk?

It's more personal opinion, but I think the main reason of using those libraries is separation of concerns. With middleware implementation to handle all async flows, we can have 3 logical parts in a react/redux app: redux part consisting of pure functions for synchrounous data flow, redux middleware part (can be redux-thunk or redux-saga) which deals with all API calls and side-effects from redux and react part to handle user interaction and communication with redux store. This way the code will be more manageable and easier for unit testing.

Am I right to say if I do not use saga then if I click 1000 times my code will rin the asnc code and wait for result 1000 times but with saga I have a way to control over it either run all in parallel(fork) or just run the last one?

Again, a self-implemented redux middleware can be use to throttle all but the last call. However, redux-saga already has an API for this case, and many more for other common async problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.