Skip to content

Commit

Permalink
Clarify terminology (#237)
Browse files Browse the repository at this point in the history
* Clarify action creator vs thunk

* Clarify what middleware does with thunks

* Tweak comment wording
  • Loading branch information
jmm authored and markerikson committed Mar 13, 2019
1 parent d5b6921 commit bacc013
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,14 @@ store.dispatch(withdrawMoney(100));
// such as an API call, or a router transition?

// Meet thunks.
// A thunk is a function that returns a function.
// This is a thunk.

// A thunk in this context is a function that can be dispatched to perform async
// activity and can dispatch actions and read state.
// This is an action creator that returns a thunk:
function makeASandwichWithSecretSauce(forPerson) {

// Invert control!
// Return a function that accepts `dispatch` so we can dispatch later.
// Thunk middleware knows how to turn thunk async actions into actions.

// We can invert control here by returning a function - the "thunk".
// When this function is passed to `dispatch`, the thunk middleware will intercept it,
// and call it with `dispatch` and `getState` as arguments.
// This gives the thunk function the ability to run some logic, and still interact with the store.
return function (dispatch) {
return fetchSecretSauce().then(
sauce => dispatch(makeASandwich(forPerson, sauce)),
Expand Down

0 comments on commit bacc013

Please sign in to comment.