I see your pain. this is an illness of front end development. There is some library or toolkit for every fart, then it gets convoluted and super hard to read. + this fashionable keywords async/await just make your brain explode.
thunk is very simple redux middleware it has 7 lines of code, so I could even paste it here':
function createThunkMiddleware(extraArgument) {
return ({ dispatch, getState }) => (next) => (action) => {
if (typeof action === 'function') {
return action(dispatch, getState, extraArgument);
}
return next(action);
};
}
the key part starts with the keyword return. This means if your action is not actually action but a function (with the thunk type signature) it returns call to that function instead of returning call next(action). This will break the chain however it does not matter as we are not dispatching the actual action but only the function (called thunk) which dispatches another action.
That is all what it does.
Now when we see what thunk does we can decipher this
dispatch(fetchUserById(userId))
.then(unwrapResult)
.then(originalPromiseResult => {})
.catch(rejectedValueOrSerializedError => {})
fetchUserById(userId) returns returns function in the form
(dispatch, getState) => {
// do something;
return somePromise;
}
which is called from the redux-thunk and the result is returned.
After that you are only chaining on that promise.