use async and await (#62)

Use async and await instead of promise chains
This commit is contained in:
mrdrogdrog 2020-05-24 22:55:06 +02:00 committed by GitHub
parent 11f01094b4
commit a5af15b278
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 92 additions and 80 deletions

View file

@ -3,33 +3,25 @@ import {LoginStatus} from "../redux/user/types";
import {setUser} from "../redux/user/methods";
import {store} from "./store";
export const getAndSetUser = () => {
getMe()
.then(expectResponseCode())
.then(response => response.json())
.then(user => {
if (!user) {
return;
}
setUser({
status: LoginStatus.ok,
id: user.id,
name: user.name,
photo: user.photo,
});
});
export const getAndSetUser = async () => {
const meResponse = await getMe();
expectResponseCode(meResponse);
const me = await meResponse.json();
if (!me) {
return;
}
setUser({
status: LoginStatus.ok,
id: me.id,
name: me.name,
photo: me.photo,
});
}
export const getBackendUrl = () => {
return store.getState().frontendConfig.backendUrl;
}
export const expectResponseCode = (code: number = 200): ((response: Response) => Promise<any>) => {
return (response: Response) => {
if (response.status !== code) {
return Promise.reject(`Response code not ${code}`);
} else {
return Promise.resolve(response);
}
}
export const expectResponseCode = (response: Response, code: number = 200) => {
return (response.status !== code);
}