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 {
const response = await fetch(
getApiUrl("/teams") getApiUrl("/teams")
) )
.then(response => response.json()) if (!response.ok) {
.then(data => { throw new Error(`http error status: ${response.status}`)
teams.value = data }
}) return await response.json()
.catch(error => { } catch (error) {
console.error('Ошибка:', error) console.error('[apiGetTeams] error:', error)
}); throw error
}
} }
export const apiAddTeam = (teamName: Ref<string>) => { export const apiAddTeam = async (teamName: string) => {
fetch( try {
const response = await fetch(
getApiUrl("/teams"), getApiUrl("/teams"),
{ {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
"teams": [{ "name": teamName.value }] "teams": [{ "name": teamName }]
}) })
} }
) )
.then(() => { teamName.value = "" }) if (!response.ok) {
.catch(error => { throw new Error(`http error status: ${response.status}`)
console.error('Ошибка:', error) }
}); return await response.json()
} catch (error) {
console.error('[apiAddTeam] error:', error)
throw error
}
} }
export const apiGetGame = (game: Ref<Game | undefined>, gameState: Ref<string>) => { export const apiGetGame = (game: Ref<Game | undefined>, gameState: Ref<string>) => {