add async await
This commit is contained in:
		
							parent
							
								
									805f5a6d4f
								
							
						
					
					
						commit
						695b5b149f
					
				@ -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) => {
 | 
			
		||||
 | 
			
		||||
@ -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(
 | 
			
		||||
export const apiGetTeams = async (): Promise<Teams> => {
 | 
			
		||||
  try {
 | 
			
		||||
    const response = await fetch(
 | 
			
		||||
      getApiUrl("/teams")
 | 
			
		||||
    )
 | 
			
		||||
  .then(response => response.json())
 | 
			
		||||
  .then(data => {
 | 
			
		||||
    teams.value = data
 | 
			
		||||
  })
 | 
			
		||||
  .catch(error => {
 | 
			
		||||
    console.error('Ошибка:', error)
 | 
			
		||||
  });
 | 
			
		||||
    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(
 | 
			
		||||
export const apiAddTeam = async (teamName: string) => {
 | 
			
		||||
  try {
 | 
			
		||||
    const response = await fetch(
 | 
			
		||||
      getApiUrl("/teams"),
 | 
			
		||||
      {
 | 
			
		||||
        method: "POST",
 | 
			
		||||
        body: JSON.stringify({
 | 
			
		||||
        "teams": [{ "name": teamName.value }]
 | 
			
		||||
          "teams": [{ "name": teamName }]
 | 
			
		||||
        })
 | 
			
		||||
      }
 | 
			
		||||
    )
 | 
			
		||||
  .then(() => { teamName.value = "" })
 | 
			
		||||
  .catch(error => {
 | 
			
		||||
    console.error('Ошибка:', error)
 | 
			
		||||
  });
 | 
			
		||||
    if (!response.ok) {
 | 
			
		||||
      throw new Error(`http error status: ${response.status}`)
 | 
			
		||||
    }
 | 
			
		||||
    return await response.json()
 | 
			
		||||
  } catch (error) {
 | 
			
		||||
    console.error('[apiAddTeam] error:', error)
 | 
			
		||||
    throw error
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const apiGetGame = (game: Ref<Game | undefined>, gameState: Ref<string>) => {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user