
Understanding redux-saga: From action creators to sagas - efunction
https://blog.logrocket.com/understanding-redux-saga-from-action-creators-to-sagas-2587298b5e71
======
ricardobeat
This fails to demonstrate any benefit. The end result, ease of implementation,
action flow and performance are the same, with a couple extra abstractions in
the middle. What does it achieve?

~~~
willriker
One key advantage of redux-saga is testability-- since sagas are essentially
functions with their arguments passed in by the saga middleware, it is easy to
inject mocked dependencies in a test environment.

~~~
rhinoceraptor
It isn't hard to create easily testable redux actions using redux-thunk. When
you configure the redux store you can use `thunk.withExtraArgument` to provide
a service object to your actions, so you can write something like this:

    
    
      const getDog = dogId => (dispatch, getState, { DogApi }) => {
        dispatch(fetchingDog(dogId))
        DogApi.getDogById(dogId, (err, dog) => {
          dispatch(err ? dogFetchError(err) : fetchedDog(dog))
        })
      }
    
      ...
    
      dispatch(getDog(123))

------
willriker
Another benefit of redux-saga not mentioned here is the ability to build
stateful sagas. For example, something like a game loop can be encoded in a
saga like this:

export default function* playGame({ getState, selectors }) { yield
take(GAME_READY);

    
    
      const refreshDelay = 1000 / FPS;
    
      while (true) { 
        yield take(START_GAME_PLAY);
    
        while (true) { 
          const {
            gameState: {
              speed,
              isPlaying,
              gameTime,
            },
          } = getState();
    
          if (!isPlaying) {
            // game play has stopped
            break;
          }
    
          const timeDelta = refreshDelay * speed;
          yield put(setGameTime(gameTime + timeDelta));
    
          // sleep
          yield call(delay, refreshDelay);
        }
      }
    }

------
anarchy8
redux-sagas are awesome. If you have a complicated web app (and most apps
aren't) then it's a godsend. It really simplifies a lot of the business logic.
You do have to be careful about performance though.

