.NET Core

How to return Promise after dispatching with Redux Thunk

It’s very common to use Promise with Redux Thunk, it sounds complicated but it simply just returns Promise.resolve(data) after dispatching to the reducers. Here is an example in the redux action to signup new user, then on Client just uses then(result) to manipulate the screen logic


export const signUpUser = user => {
return dispatch => {
return fetch(`http://840dff44.ngrok.io/identity/authentication/signup`, {
method: "post",
headers: {
"Content-Type": "application/json",
Accept: "json"
},
body: JSON.stringify(user)
})
.then(res => res.json())
.then(data => {
if (data.isSuccess) {
dispatch({
type: TOKEN_UPDATED,
payload: data.token
});
dispatch({
type: TOKEN_EXPIRED_ON,
payload: data.tokenExpiredOn
});
}
return Promise.resolve(data);
})
.catch(error => {
console.log(error);
return Promise.reject(error);
});
};
};

Posted by justinpham in .NET Core, AI, React - Redux, Solution Architecture, 0 comments