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,13 +3,13 @@ import {BackendConfigState} from "../redux/backend-config/types";
import {expectResponseCode, getBackendUrl} from "../utils/apiUtils";
export const getBackendConfig = async () => {
return fetch(getBackendUrl() + '/backend-config.json')
.then(expectResponseCode())
.then(response => response.json() as Promise<BackendConfigState>);
const response = await fetch(getBackendUrl() + '/backend-config.json');
expectResponseCode(response);
return await response.json() as Promise<BackendConfigState>;
}
export const getFrontendConfig = async () => {
return fetch('config.json')
.then(expectResponseCode())
.then(response => response.json() as Promise<FrontendConfigState>);
const response = await fetch('config.json');
expectResponseCode(response)
return await response.json() as Promise<FrontendConfigState>;
}

View file

@ -5,7 +5,7 @@ export const getMe = async () => {
}
export const postEmailLogin = async (email: string, password: string) => {
return fetch(getBackendUrl() + "/login", {
const response = await fetch(getBackendUrl() + "/login", {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
@ -19,13 +19,14 @@ export const postEmailLogin = async (email: string, password: string) => {
email: email,
password: password,
})
})
.then(expectResponseCode())
.then(response => response.json());
});
expectResponseCode(response);
return await response.json();
}
export const postLdapLogin = async (username: string, password: string) => {
return fetch(getBackendUrl() + "/auth/ldap", {
const response = await fetch(getBackendUrl() + "/auth/ldap", {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
@ -40,12 +41,13 @@ export const postLdapLogin = async (username: string, password: string) => {
password: password,
})
})
.then(expectResponseCode())
.then(response => response.json());
expectResponseCode(response)
return await response.json();
}
export const postOpenIdLogin = async (openId: string) => {
return fetch(getBackendUrl() + "/auth/openid", {
const response = await fetch(getBackendUrl() + "/auth/openid", {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
@ -59,6 +61,7 @@ export const postOpenIdLogin = async (openId: string) => {
openId: openId
})
})
.then(expectResponseCode())
.then(response => response.json());
expectResponseCode(response)
return await response.json();
}