add async await

This commit is contained in:
Владимир Фёдоров 2025-06-30 23:48:39 +07:00
parent 805f5a6d4f
commit 695b5b149f
2 changed files with 42 additions and 38 deletions

View File

@ -15,12 +15,9 @@ const game = ref<Game | undefined>()
const teams = ref<Teams>({ teams: [] }) const teams = ref<Teams>({ teams: [] })
const teamName = ref("") const teamName = ref("")
function addTeam() { async function addTeam() {
apiAddTeam(teamName) await apiAddTeam(teamName.value)
} teamName.value = ""
function getGame() {
apiGetGame(game, gameState)
} }
function startGame() { function startGame() {
@ -32,12 +29,12 @@ function stopGame() {
} }
let intervalId = 0 let intervalId = 0
onMounted(() => { onMounted(async () => {
apiGetTeams(teams) teams.value = await apiGetTeams()
intervalId = setInterval(() => { intervalId = setInterval(async () => {
apiGetTeams(teams) teams.value = await apiGetTeams()
getGame() apiGetGame(game, gameState)
}, 2000); }, 2000);
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {

View File

@ -3,33 +3,40 @@ import { getApiUrl } from './net';
import type { Game, Teams } from './models'; import type { Game, Teams } from './models';
import { downloadData } from './qr'; import { downloadData } from './qr';
export const apiGetTeams = (teams: Ref<Teams>) => { export const apiGetTeams = async (): Promise<Teams> => {
fetch( try {
getApiUrl("/teams") const response = await fetch(
) getApiUrl("/teams")
.then(response => response.json()) )
.then(data => { if (!response.ok) {
teams.value = data throw new Error(`http error status: ${response.status}`)
}) }
.catch(error => { return await response.json()
console.error('Ошибка:', error) } catch (error) {
}); console.error('[apiGetTeams] error:', error)
throw error
}
} }
export const apiAddTeam = (teamName: Ref<string>) => { export const apiAddTeam = async (teamName: string) => {
fetch( try {
getApiUrl("/teams"), const response = await fetch(
{ getApiUrl("/teams"),
method: "POST", {
body: JSON.stringify({ method: "POST",
"teams": [{ "name": teamName.value }] body: JSON.stringify({
}) "teams": [{ "name": teamName }]
})
}
)
if (!response.ok) {
throw new Error(`http error status: ${response.status}`)
} }
) return await response.json()
.then(() => { teamName.value = "" }) } catch (error) {
.catch(error => { console.error('[apiAddTeam] error:', error)
console.error('Ошибка:', error) throw error
}); }
} }
export const apiGetGame = (game: Ref<Game | undefined>, gameState: Ref<string>) => { export const apiGetGame = (game: Ref<Game | undefined>, gameState: Ref<string>) => {
@ -71,9 +78,9 @@ export const apiStopGame = (gameState: Ref<string>) => {
getApiUrl("/game/stop"), getApiUrl("/game/stop"),
{ method: "POST" } { method: "POST" }
) )
.catch(error => { .catch(error => {
console.error('Ошибка:', error) console.error('Ошибка:', error)
}); });
} }