Reputation: 1826
So, I have got an action creator (that returns a function - I use redux-thunk). Inside the function the creator returns, I call a dispatch method and chain the then
and catch
methods.
Here how it looks like:
export function actionCreator(someData) {
(dispath, getState) => {
return dispatch(someAction)
.then(resp => {
//do something
// GO TO CATCH
})
.catch(err => {
return err;
})
}
}
You see the GO TO CATCH comment over there? So, how can I go to the catch block from there?
Thank you!
Upvotes: 2
Views: 4924
Reputation: 11557
If you want to jump from the body of a .then()
to the following .catch()
, the easiest way is to throw an error:
throw new Error('I meant to blow up here.');
The error you throw is what will be passed to the body of the .catch()
(the err
variable, in your case).
Please note that your example already looks suspect though: your catch block is catching an error and then returning it as if the error was a regular value, which means any upstream promise-based handling will assume the dispatch was successful. Are you sure that's what you want?
Upvotes: 4