State Management · Middle · Question 55 of 75
How does a component get the thunk's result or catch its error?
try {
const user = await dispatch(saveUser(data)).unwrap();
navigate(`/users/${user.id}`);
} catch (err) {
showError(err.message);
}- ✓Correct answer: Call
.unwrap()on the promise returned bydispatch, which resolves with the payload or throws the error - 2Read the result from
useSelectorimmediately - 3Thunks can't return results
Explanation
Without unwrap, the promise always resolves with the final action (fulfilled or rejected), which is easy to mishandle.