Contenu connexe Similaire à Redux Thunk - Fu - Fighting with Async (20) Redux Thunk - Fu - Fighting with Async9. const action = {
type: 'HOLIDAYCHECK_POZNAN',
recruiting: true
};
dispatch(action)
10. function checkHolidayCheck() {
return function (dispatch, getState) {
return fetch('http://bit.ly/hcpoz-fb')
.then((response) => {
dispatch({
type: 'HOLIDAYCHECK_POZNAN',
recruiting: response.recruiting
})
});
}
}
dispatch(checkHolidayCheck())
14. function typicalAction() {
return function (dispatch, getState) {
dispatch({ type: 'ACTION_ATTEMPT' });
// do something and resolve response
if (response.status === 'ok') {
return dispatch({ type: 'ACTION_SUCCESS' });
} else {
return dispatch({ type: 'ACTION_FAILURE' });
}
}
}
dispatch(typicalAction())
21. const dispatchPromiseResult = (dispatch, { actionType, promise }) => {
dispatch(firedAction(actionType));
return Promise.resolve(promise)
.then((data) => dispatch(receivedData(actionType, data)))
.catch((err) => dispatch(receivedError(actionType, err)));
};
Together 统
22. function checkHolidayCheck() {
return function (dispatch, getState) {
return fetch('http://bit.ly/hcpoz-fb')
.then((response) => {
dispatch({
type: 'HOLIDAYCHECK_POZNAN',
recruiting: response.recruiting
})
});
}
}
dispatch(checkHolidayCheck())
Before
23. function checkHolidayCheck() {
return function (dispatch, getState) {
return dispatchPromiseResult(dispatch, {
actionType: 'HOLIDAYCHECK_POZNAN',
promise: fetch('http://bit.ly/hcpoz-fb')
})
}
}
dispatch(checkHolidayCheck())
After
25. function seeYouNextTime() {
return function (dispatch, getState, extraArgumentIsGreat) {
return dispatchPromiseResult(dispatch, {
actionType: 'THIS IS THE END,
promise: fetch('http://bit.ly/hcpoz-fb')
})
}
}
dispatch(seeYouNextTime())
Extra argument