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

Redux: Wrapping dispatch() to Recognize Promises

Redux: Wrapping dispatch() to Recognize Promises

6:29
We will learn how to teach dispatch() to recognize Promises so that we can move the async logic out of the components into asynchronous action creators.
Watch this lesson now
Avatar
egghead.io

We will learn how to teach dispatch() to recognize Promises so that we can move the async logic out of the components into asynchronous action creators.

Avatar
Howon

Couple of questions :)
1. when you call receiveTodos(..), how come you don't have to wrap dispatch(receiveTodos(...))?
2. when you do then(rawDispatch), does it do rawDispatch(response) when the promise is ready?

Avatar
Jiaming
  1. when you call receiveTodos(..), how come you don't have to wrap dispatch(receiveTodos(...))?

Because he passed the whole actions object into the connect function as the second argument. Since he imported the whole actions namespace into the actions object by import * as actions, the second argument, which is mapDispatchToProps, will do the dispatch wrapping for all of the exported functions in actions/index.js.

  1. when you do then(rawDispatch), does it do rawDispatch(response) when the promise is ready?

I also have problem understanding this one.

In reply to Howon
Avatar
Jiaming
  1. when you do then(rawDispatch), does it do rawDispatch(response) when the promise is ready?

I've figured out how this one works. When the fetchTodos actions is dispatched, it would wait for the response and build a receiveTodos action and return it as a promise. Since it's a promise, the addPromiseSupportToDispatch would chain the then(rawDispatch) at the end of it. Therefore, the rawDispatch would use the receivedTodos action returned by the previous promise as its argument and dispatch the receiveTodos action to the store.

In reply to Howon
Avatar
Ningze

For question No.1 : You don't need to call dispatch(receiveTodos(...)) because receiveTodos(...) is called in the first then() of Promise, and the returned value (which is an action) of the method in the first then() will be automatically fed into the input parameter of the method in the second then(), which is the rawDispatch.

For question No.2: No, it will do rawDispatch(action), not the response, because the response will be converted into an action by calling receiveTodos in the first then(), see the answer to question No.1

I think you are not quite clear about how Promise works. Your questions have nothing to do with redux or react.

In reply to Howon
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?