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 teamName = ref("")
function addTeam() {
apiAddTeam(teamName)
}
function getGame() {
apiGetGame(game, gameState)
async function addTeam() {
await apiAddTeam(teamName.value)
teamName.value = ""
}
function startGame() {
@ -32,12 +29,12 @@ function stopGame() {
}
let intervalId = 0
onMounted(() => {
apiGetTeams(teams)
onMounted(async () => {
teams.value = await apiGetTeams()
intervalId = setInterval(() => {
apiGetTeams(teams)
getGame()
intervalId = setInterval(async () => {
teams.value = await apiGetTeams()
apiGetGame(game, gameState)
}, 2000);
router.beforeEach((to, from, next) => {

View File

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