Promised way to async flows with Redux

Freedom to async when there’s no one true way

Let’s get to 88 mph
  1. We don’t dispatch actions with apiCall property to the store, so their processing ends just here.
  2. Middleware should go among the first in the middleware chain to prevent fancy things happening to not-dispatched-further actions. Simplest example is logging actions with async calls — they don’t get dispatched to store.
  3. If you convert promise-based code to async/await note that now you’ll have promise in return from next(action) call and manage this effect accordingly.
  4. Middleware allows you to put all the things related to handling API calls in one place and reduce (pun not intended) amount of copy-pasting in apiCall functions.
  5. Request-related action dispatched to store has modified types. I’ve came up with handy utility functions request, response and fail and use them both in middleware and in reducers. They hide constant postfixes and are nice.
Usable and nice, aren’t they?

Don’t be afraid to try your own way from the start — things in redux are much more hackable than they may seem

--

--

JavaScript kitten by day, Code Hipsters Editor-in-Chief by night

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store