Why we need Redux-thunk?

Why we need redux-thunk, if you can work like this:
import {START, SUCCESS, FAIL} from '../constants'

export default store => next => action => {
 const {callAPI, type, ...rest} = action
 if (!callAPI) return next(action)

 next({ 
 ...rest, type: type + START
})
fetch(callAPI)
 .then(res => res.json())
 .then(response => next({...rest, type: type + "SUCCESS", response}))
 .catch(error => next({...rest, type: type, + FAIL, error}))
}
June 7th 19 at 14:26
2 answers
June 7th 19 at 14:28
Solution
The question sounds like: Why you need a BMW, if you have a Mercedes. Answer: as you like, and work. From such considerations redux-thunk and not included in the standard delivery redux.
Why this library is so popular? - Joesph66 commented on June 7th 19 at 14:31
well it's a little more versatile, and it is not necessary to write :) - cortney.Kuhic commented on June 7th 19 at 14:34
understood, thank you very much for the reply :) - Joesph66 commented on June 7th 19 at 14:37
June 7th 19 at 14:30
Solution
honestly straning question is trivial, but so as not to scare the Masa was well written ofitsialnoe solution which is enough for many cases

function createThunkMiddleware(extraArgument) {
 return ({ dispatch, getState }) => next => action => {
 if (typeof action === 'function') {
 return action(dispatch, getState, extraArgument);
}

 return next(action);
};
}

const thunk = createThunkMiddleware();
thunk.withExtraArgument = createThunkMiddleware;

export default thunk;


for those who do not like to climb on the source github the above code is the entire code redyx-thunk + there are also tests which are slightly longer )
there is no magic, I will not say that your example is better, but if you like to use, but be prepared to answer the question more farmers, and th thunk or saga not taken ? it all know, and what profit tomago decisions ?
it matters not Concerto at your code, but in quite a justification questions when someone drags in a project my bike is all known solutions

Find more questions by tags ReduxJavaScriptReact