Created
August 3, 2020 15:37
-
-
Save Rajatgms/de4941803367f97634c2bd0a1833f34e to your computer and use it in GitHub Desktop.
thunkActionCreator Response handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { placeOrderThunk } from '../slice/cartSlice'; | |
import { startLoader } from '../slice/loaderSlice'; | |
import { error, success } from '../slice/notifySlice'; | |
import { unwrapResult } from '@reduxjs/toolkit'; | |
export const handleCartPaymentAsyncAction = () => { | |
return dispatch => { | |
dispatch(startLoader(true)); | |
dispatch(placeOrderThunk()) | |
// Always return resolved promise with error or payload hence used unwrapResult | |
// extract the payload or error from the action and return or throw the result | |
.then(unwrapResult) | |
.then(result => { | |
dispatch(success(result.message)); | |
}) | |
.catch(e => { | |
dispatch(error(e.message)); | |
}) | |
.finally(() => dispatch(startLoader(false))); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment