Skip to content

Instantly share code, notes, and snippets.

@Rajatgms
Created August 3, 2020 15:37
Show Gist options
  • Save Rajatgms/de4941803367f97634c2bd0a1833f34e to your computer and use it in GitHub Desktop.
Save Rajatgms/de4941803367f97634c2bd0a1833f34e to your computer and use it in GitHub Desktop.
thunkActionCreator Response handling
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