The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Redux: Dispatching Actions Asynchronously with Thunks

Redux: Dispatching Actions Asynchronously with Thunks

4:44
We will learn about “thunks”—the most common way to write async action creators in Redux.
Watch this lesson now
Avatar
egghead.io

We will learn about “thunks”—the most common way to write async action creators in Redux.

Avatar
Dmitry

What about redux-saga? It looks like nice alternative to thunks.

Avatar
Sourav

I cannot seem to make the Thunk UMD work.I was trying to play in JSBin.It gives me undefined.

https://jsbin.com/fujuqe/15

In reply to egghead.io
Avatar
Ken Snyder

I love thunks and use them regularly but I find myself often wanting "composable" or "thennable" thunks. Is that a thing? Let me explain what I'm looking for ... I want to call two action creators and both are thunks but they have an synchronous dependency between them (aka, B must follow A).

I'd want the code to look something like:

dispatch(actionCreator.A())
.then(dispatch(actionCreator.B())

Avatar
Charlie

Why explicitly pass store.dispatch, rather than next, to action? For example, this is the recommended code:

const thunk = (store) => (next) => (action) => {
  (typeof action === 'function') ?
    action(store.dispatch) :
    next(action);

Is the above objectively better than:

const thunk = (store) => (next) => (action) => {
  (typeof action === 'function') ?
    action(next) : // Passing next instead of store.dispatch
    next(action);

Thanks!

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?