Where does thunk middleware come into place? This is the order of the middlewares that I see this going through right now but I get stuck on where Thunk comes in:
- dispatches action that has
promise
value set to true - goes to thunk, thunk doesn't do anything with it because it's an object, not function
- goes to promiseErrorMiddleware, which gets store from
applyMiddlware
and returns a function. - does this function get intercepted by Thunk even though it was returned, and not dispatched? who exactly runs this function that will return the next function with the action as an argument? who will then run that final function?
Store
const store = createStore(
rootReducer,
applyMiddleware(thunkMiddleware, promiseErrorMiddleware, dataTrafficMiddleware)
)
actionCreator
dispatch({url: requestURL, promise: true})
promiseErrorMiddleware & dataTrafficMiddleware
const promiseErrorMiddleware = function (store) { //where is store from, applyMiddleware?
return function (next) { //where is next from?
return function (action) {
if (!action.promise) {
return next(action)
} else {
return fetch(action.url).then(function (data) {
...
next({data, needDirection: true})
})
}
}
}
}
const dataTrafficMiddleware = function (store) {
return function (next) {
return function (action) {
if (!action.needDirection) {
return next(action)
} else {
//dispatch regular action with type and data
}
}
}
}
}