mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-28 22:15:12 -04:00
use async and await (#62)
Use async and await instead of promise chains
This commit is contained in:
parent
11f01094b4
commit
a5af15b278
7 changed files with 92 additions and 80 deletions
|
@ -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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue