I know about anti-pattern that can be happened while using Promises in javascript.
This is my scenario.
I have a request function that return a Promise.
I want to use this inside another function and execute some code in then
and then resolve it or reject it in some condition:
export const login = (req: IRegisterAuthReq): Promise<IUserTokenResponse> => {
return new Promise((resolve, reject) => {
AuthApi.login(req)
.then(response => {
if (response.success) {
store.dispatch(setUserLoggedIn(true));
resolve(response.data as IUserTokenResponse);
} else {
reject();
}
})
.catch(reject)
.finally(() => {
store.dispatch(setAppLoading(false));
});
});
};
This is my code, I call AuthApi.login function and execute some code and then resolve value.
and then use it like this:
login(req)
.then(authoredSuccessful)
.catch(authoredUnSuccessful);
I want to know if this approach is anti-pattern or bad, and if it is how can I achieve this functionality in best way.