Redux Middleware Comparison: Thunk, Sagas, Observable & Loop

by Theo on 30.07.2018

Redux is probably by far the most popular library to handle application state in React-apps, and while there are other solutions like MobX out there, we at Sandstorm currently fully rely on Redux. Vanilla Redux alone is great to manage state, but does not have a built in way to handle asynchronous operations like AJAX-Requests. There are quite a few very different middlewares out there to help with these tasks and their internal concepts vary quite a bit. 

This blog post is supposed to shed some light on some of the more popular middlewares for async handling - namely Redux-Thunk, Redux-Saga, Redux-Observable and Redux-Loop -  and the basic concepts behind them. I am also going to ramble a bit about when their usage might be appropriate and about their respective pros and cons.

While I am going to showcase each library with a basic example, I will keep it fairly high level and wont go into detail about advanced operations, like cancellation or throttling (these might be the subject of a follow up blog post in the future). Therefore I am going to show how to fetch some sample data with each middleware. I am not going to depict the process of integrating each middleware but rather focus on the way each middleware works. While the examples are fairly basic I assume that you already have a good grasp of how Redux itself works - otherwise I suggest familiarizing yourself with its concepts before continuing to read this blog post.

Table of Contents

  1. Redux-Thunk
  2. Redux-Observable
  3. Redux-Saga
  4. Redux-Loop
  5. Conclusion


Redux-Thunk is usually the first library that is suggested when it comes to basic async handling and is even promoted as the standard way by Redux itself. Its concept is pretty straight forward: while Redux only allows you to dispatch plain action objects, Redux-Thunk also allows you to dispatch functions. This enables us to encapsulate additional logic as well as asynchronous operations and respond by dispatching different actions.


  1. // ACTION TYPES -------------------------------------------------------------
  6. export const actionTypes = {
  10. }
  13. // (we use the "redux-actions"-library for easier creation of basic actionCreators)
  14. const syncActionCreators = createActions({
  15. FETCH_USERS_START : () => {},
  16. FETCH_USERS_SUCCESS: users => ( { users } ),
  17. FETCH_USERS_FAILURE: errorMessage => ( { errorMessage } )
  18. })
  21. const asyncActionCreators = {
  22. fetchUsers: () => {
  23. // We return a function which will get our store.dispatch handed by the thunk middleware
  24. return (dispatch) => {
  25. // We manually dispatch our START action to let the app know we're loading user data
  26. dispatch(syncActionCreators.fetchUsersStart())
  28. // Here we make our actual AJAX-request and depending on if the promise
  29. // resolves/rejects we dispatch our success or error action respectively
  30. return fetch("https://jsonplaceholder.typicode.com/users")
  31. .then(response => {
  32. if (response.ok) {
  33. return response.json()
  34. } else {
  35. throw new Error('damn :(')
  36. }
  37. })
  38. .then(data => dispatch(syncActionCreators.fetchUsersSuccess(data)))
  39. .catch(error => dispatch(syncActionCreators.fetchUsersFailure(error)))
  40. }
  41. }
  42. }
  44. // ACTIONS -------------------------------------------------------------
  45. export const actions = {
  46. ...syncActionCreators,
  47. ...asyncActionCreators
  48. };
  50. // REDUCERS -------------------------------------------------------------
  51. const usersReducer = handleActions({
  52. [FETCH_USERS_START]: (state, action) => Object.assign({}, state, {
  53. fetching: true
  54. }),
  55. [FETCH_USERS_SUCCESS]: (state, action) => Object.assign({}, state, {
  56. fetching: false, items: action.payload.users
  57. }),
  58. [FETCH_USERS_FAILURE]: (state, action) => Object.assign({}, state, {
  59. fetching: false, errorMessage: action.payload.errorMessage
  60. })
  61. }, { items: [], fetching: false, errorMessage: '' })

Whenever our fetchUsers()-function is dispatched, its inner function will receive the stores dispatch method. At this point no actual action has been dispatched and our state remains unchanged. Before we attempt our API-request using the fetch-API we therefore should dispatch our fetchUsersStart-action.

This will tell our usersReducer to set the fetching-state to true and we could now for example show a loading spinner inside our user interface. Then we make our actual request, which eventually leaves us with a promise that we can handle like usual. If the promise resolves we will dispatch our fetchUsersSuccess-action. If it rejects, we dispatch the fetchUsersFailure-action. - simple, right?

Pros and Cons of Redux-Thunk

Redux-Thunk is really easy to learn as there are literally no new concepts involved at all. Everything is basic javascript and therefore makes it easy to pick up and integrate into your application. On the flipside it does not scale that well and as the amount of your async logic grows you might end up with rather complicated code.

Furthermore it might no longer be obvious, which action creators are pure and which have sideeffects attached to them, which further complicates the flow of your application logic (although naming conventions might help to lessen this issue). Also there is no built in way to handle advanced tasks like throttling, debouncing or cancellation.


According to its number of GitHub-Stars its fair to asume that Redux-Saga is probably the most popular solution when it comes to async middlewares. While its underlying concepts are far more complicated, than for example Redux-Thunk, it is way more powerful as well.

Redux-Saga makes heavy use of a rather new feature of the ECMAScript-standard called generator functions . In simple terms these are basically functions which can be paused, yielding a value to its caller. The caller in turn processes the given value and then resumes the generator function handing it back the processed value (generator functions basically expose an Iterator-like interface).

Within Redux-Saga generator functions are - surprise, surprise - by convention called sagas. The saga middleware exposes a set of helper functions to create declarative effects (plain javascript objects) that can be yielded by our sagas. The middleware will then handle the objects yielded behind the scenes.

The call()-helper for example lets us yield effects describing a function and its arguments to Redux-Saga. The described function will be called with the given arguments by the middleware and be processed further depending on its return value. If the result is a generator or iterator as well, it will be run like the parent generator (this basically enables saga-composition). If on the other hand the result is a promise, the middleware will first resolve the promise and then hand its result back to the saga that yielded the effect. Errors can simply be handled by surrounding your logic with try/catch-statements.


  1. import { takeLatest, put, call } from 'redux-saga/effects';
  3. // ACTION TYPES ---------------------------------------------------------
  8. const actionTypes = {
  12. }
  14. // ACTIONS -------------------------------------------------------------
  15. const actions = createActions({
  16. FETCH_USERS_START : () => {},
  17. FETCH_USERS_SUCCESS: users => ( { users } ),
  18. FETCH_USERS_FAILURE: errorMessage => ( { errorMessage } )
  19. });
  22. // REDUCERS -------------------------------------------------------------
  23. const usersReducer = handleActions({
  24. [FETCH_USERS_START]: (state, action) => Object.assign({}, state, {
  25. fetching: true
  26. }),
  27. [FETCH_USERS_SUCCESS]: (state, action) => Object.assign({}, state, {
  28. fetching: false, items: action.payload.users
  29. }),
  30. [FETCH_USERS_FAILURE]: (state, action) => Object.assign({}, state, {
  31. fetching: false, errorMessage: action.payload.errorMessage
  32. })
  33. }, { items: [], fetching: false, errorMessage: '' })
  36. // SAGAS -----------------------------------------------------------------
  37. // The asterisk denotes that this is a generator function
  38. function * fetchUsersSaga() {
  39. // Spawns the specified generator whenever an action of the type FETCH_USERS_START
  40. // flows through our middleware. Running sagas from previous FETCH_USERS_START-actions
  41. // are cancelled automatically
  42. yield takeLatest(actionTypes.FETCH_USERS_START, fetchUsers)
  43. }
  45. function * fetchUsers() {
  46. try {
  47. // here we describe our api-request as effect
  48. const users = yield call(() => fetch("https://jsonplaceholder.typicode.com/users")
  49. .then(resp => resp.json()));
  51. // calling our action creator we create our fetchUsersSuccess-action object.
  52. // The 'put()' helper instructs Redux-Saga to dispatch the action on our redux store
  53. yield put(actions.fetchUsersSuccess(users));
  54. } catch (e) {
  55. yield put(actions.fetchUsersFailure("damn :("));
  56. }
  57. }

This time we have no additional logic attached to our action creators or reducers. Instead our async logic resides completely separate from our other redux logic. The takeLatest()-helper takes two arguments - pattern to match and a generator function. Now when an action of the type FETCH_USERS_START reaches our fetchUsersSaga() the fetchUsers()-generator will be spawned. If there is already an instance of fetchUsers() running it will be cancelled in favor of the new instance.

As described above we yield an effect object created with the call()-effect creator containing the invokation of the fetch()-API to the middleware. Redux-Saga in turn handles the promise returned by the API and either hands us the resolved response or throws an error we can handle inside our catch()-block. Note that the use of the fetch-API requires us to also chain an additional .then()-block to our fetch()-call where we convert the fetch result to our actual JSON-response.  Now we can use the put()-effect creator to instruct the middleware to dispatch a FETCH_USERS_SUCCESS-action which updates our store with the user data from the fetch-response.

Pros and Cons of Redux-Saga

Redux-Saga is an immensely powerful tool that comes packed with a lot of useful helper functions. E.g. it already has built in ways to handle advanced tasks like throttling, debouncing, race conditions and cancellation. It is also very well documented. The downsides are that you really have to understand generator functions first. While these are a powerful tool in you belt, they are currently rarely used outside of the Redux-Saga world, which means that this particular skill might not be as transferable to other fields as lets say Redux-Observable, which is built on reactive streams.

Redux-Saga code also tends to be rather imperative. Depending on your personal preferences this might be either a pro or a con. Another painpoint with sagas is testing. It is certainly doable, but can be quite confusing at times. Because sagas live outside of your other redux logic it also might not always be obvious, where sideeffects actually happen. 


Another popular solution for handling async operations is Redux-Observable. As the name already suggests Redux-Observable is built around the concept of observables and reactive streams. It is basically just a thin middleware wrapper around RxJS with just a few additional helper methods. Therefore you also need to add RxJS to your application if you want to use Redux-Observable.

Similar to how sagas are handled, sideeffects in Redux-Observable are separated from your other redux code and are handled in so called epics. An epic is basically just a function which takes a stream of actions and returns a new stream of actions. Note though, that an action already has flown through your reducers at the time it arrives in your epics.


  1. import { of } from 'rxjs';
  2. import { map, mergeMap, catchError } from 'rxjs/operators'
  3. import { ajax } from 'rxjs/ajax';
  4. import { combineEpics, ofType } from 'redux-observable';
  6. // ACTION TYPES ------------------------------------------------------------
  11. const actionTypes = {
  15. }
  18. // ACTIONS -------------------------------------------------------------
  19. const actions = createActions({
  20. FETCH_USERS_START : () => {},
  21. FETCH_USERS_SUCCESS: users => ( { users } ),
  22. FETCH_USERS_FAILURE: errorMessage => ( { errorMessage } )
  23. });
  26. // REDUCERS -------------------------------------------------------------
  27. const usersReducer = handleActions({
  28. [FETCH_USERS_START]: (state, action) => Object.assign({}, state, {
  29. fetching: true
  30. }),
  31. [FETCH_USERS_SUCCESS]: (state, action) => Object.assign({}, state, {
  32. fetching: false, items: action.payload.users
  33. }),
  34. [FETCH_USERS_FAILURE]: (state, action) => Object.assign({}, state, {
  35. fetching: false, errorMessage: action.payload.errorMessage
  36. })
  37. }, { items: [], fetching: false, errorMessage: '' })
  40. // EPICS -------------------------------------------------------------
  41. // Our action is not a regular redux action, but an observable stream of actions
  42. const fetchUserEpic = action => action.pipe(
  43. // Reacts to actions of type 'FETCH_USERS_START'
  44. ofType(actionTypes.FETCH_USERS_START),
  45. // Flattens created observable streams inside (is an alias for flatMap)
  46. mergeMap(() =>
  47. // Makes an ajax request and creates a new observable stream from the response
  48. ajax.getJSON("https://jsonplaceholder.typicode.com/users").pipe(
  49. // Observable value that will be returned if no error was thrown
  50. map(response => actions.fetchUsersSuccess(response)),
  51. // Observable value that will be returned if an error was thrown
  52. // 'of' creates a new observable Stream
  53. catchError(() => of(actions.fetchUsersFailure('damn :(')))
  54. )
  55. ),
  56. );

Whenever we dispatch an action, Redux-Observable takes the action after it has been dispatchted to the store, converts it into an observable stream and hands it to our epics. We can then use functional reactive programming magic to react to each action, e.g. filter by action type, add delays, or dispatch other actions in response.

In our example above our fetchUserEpic()-function filters the incoming stream of actions by our FETCH_USERS_START type. For each action of said type an AJAX-request is made and its result is mapped to another observable fetchUsersSuccess-action (or fetchUsersFailure on error) which will then be dispatched on our store. There are a few things to note here, though. For our AJAX-request we use the RxJS ajax-helper.

This helper makes the request and also turns its result into another observable stream. This resulting stream is then piped into the map() operator or on error the catchError()-operator. Because we now have an observable stream inside an observable stream - our stream of incoming actions basically contains the stream of resulting actions - we have to merge both streams into one stream. That's what the mergeMap()-operator does. It takes each incoming filtered action, maps it to the ajax-helper and flattens its result into another action that is part of the parent stream.

Pros and Cons of Redux-Observable

Redux-Observable basically shares most of the pros and cons of Redux-Saga. It ist very powerful, has cancellation, throttling etc. already baked in, but comes with a steep learning curve and somewhat esoteric testing via a testScheduler. Another downside to Redux-Observable is, that your async logic again is separated from your other redux related code.


Redux-Loop follows completely differenct concepts compared to the other libraries in this article. As some of you may know, Redux was heavily influenced by a small functional programming language called Elm. Redux-Loop is basically the missing link to make redux behave almost like Elm does. Instead of handling async operations separate from your other redux logic, they become part of your reducers. In practice your reducers are no longer able to just return a new state, but also to describe what will happen next by declaring so called commands.


  1. import { createActions, handleActions } from 'redux-actions';
  2. import { combineReducers, loop, Cmd } from 'redux-loop';
  3. import { createSelector } from 'reselect';
  6. // ACTION TYPES -------------------------------------------------------------
  11. export const actionTypes = {
  15. }
  18. // ACTIONS -------------------------------------------------------------
  19. export const actions = createActions({
  20. FETCH_USERS_START : () => {},
  21. FETCH_USERS_SUCCESS: users => ( { users } ),
  22. FETCH_USERS_FAILURE: errorMessage => ( { errorMessage } )
  23. });
  26. const fetchUsers = () => fetch("https://jsonplaceholder.typicode.com/users")
  27. .then(resp => resp.json())
  28. // We might wrap our sideeffects inside an object to make their usage even
  29. // more explicit
  30. const sideEffects = {
  31. fetchUsers
  32. }
  34. // REDUCERS -------------------------------------------------------------
  35. const usersReducer = handleActions({
  36. // Note how we do not just return our new state, but make a call to the loop()-helper instead
  37. [FETCH_USERS_START]: (state, action) => loop(
  38. // State to return
  39. Object.assign({}, state, { fetching: true }),
  40. // Cmd that declaratively describes which function to run (our sideeffect)
  41. // and which respective actions to call on either success or failure
  42. Cmd.run(sideEffects.fetchUsers, {
  43. successActionCreator: actions.fetchUsersSuccess,
  44. failActionCreator: () => actions.fetchUsersFailure('damn :(')
  45. })
  46. ),
  47. [FETCH_USERS_SUCCESS]: (state, action) => Object.assign({}, state, {
  48. fetching: false, items: action.payload.users
  49. }),
  50. [FETCH_USERS_FAILURE]: (state, action) => Object.assign({}, state, {
  51. fetching: false, errorMessage: action.payload.errorMessage
  52. })
  53. }, { items: [], fetching: false, errorMessage: '' })
  55. // IMPORTANT: We now use the redux-loop version of combineReducers instead of reduxs
  56. const dataReducer = combineReducers({
  57. users: usersReducer,
  58. posts: (state = {}, action) => state
  59. });

In this example we declare a separate function to run our sideeffect, which should be invoked, whenever we dispatch a fetchUsersStart()-action. Inside our reducer we no longer just return our new state, but make a call to the loop()-helper function instead. This function takes two arguments: 1. the actual new state to return and 2. a command object describing effects to be run. In our case we describe a runnable command with Cmd.run() and specify our fetchUser()-function as effect to be executed. Furthermore we set the successActionCreator() and failActionCreator()-optionsThe former will be invoked if our fetch-promise resolves and the latter if it rejects.

Pros and Cons

Because our async handling resides directly next to our other redux logic it is really easy to follow the flow of actions.  Commands just declaratively describe effects and so our reducers as well as our action creators remain pure functions. This makes our code easily testable (Redux-Loop comes with a few helper functions for testing commands as well).

Another big pro is that there are just a few new concepts to learn. I assume that most developers will be able to pick up Redux-Loop almost as quickly as they would pick up Redux-Thunk. One major downside compared to Redux-Observable and Redux-Saga is, that Redux-Loop does not come with any additional helpers for cancellation, throttling or debouncing. So by default it is not quite as powerful as its major competitors.


If you need something powerful and battletested you can't really go wrong with either Redux-Saga or Redux-Observable. Both have a similar feature set and help you modelling complex async data flows. We at Sandstorm will probably continue using our approach of  sagas as  finite state-machines for most projects. For smaller personal projects I will probably use Redux-Loop, because I am a big fan of its mental model, which to me feels very natural to use.

The one library I really would not recommend to use is Redux-Thunk. Yes, it is easy to use, but in my experience it scales quite badly and often leads to brittle code. In my opinion action creators should remain pure and predictable and I do not want to guess if an action creator has any additional logic attached to it. If you just need something small, that is easy to integrate, I would recommend to use Redux-Loop instead. Of course as always your mileage may vary.