[React] await - async

펭귄안녕·2024년 10월 31일
0

React

목록 보기
4/5
post-thumbnail
function test1(){

    console.log(1)

    axios.get('/test1')
    .then(res=>{
      console.log(3)
      console.log(res.data)
      console.log(4)
    })
    .catch(error=>console.log(error))

    console.log(5)

}

function test1(){

    let cnt=1;

    axios.get('/test1')
    .then(res=>{
      
      console.log(res.data)
      cnt=3;
      
    })
    .catch(error=>console.log(error))

    console.log(cnt)

}

await > 비동기를 동기화시켜주는 명령어
async > 동기를 비동기화
await 명령어는 반드시 async 명령어와 함께 사용

// async가 붙으면 함수가 비동기 방식으로 진행 
async function test2(){
  // await 명령어 사용 시 동기 방식으로 진행 

    try{
      let cnt=''
      const res = await axios.get('/test1')
      cnt=res.data
      console.log(cnt) // test1
    }catch(error){
      console.log(error)
    }

}
function test3(){
    console.log(1)
    test2()
    console.log(2)
}

export const getList = async ()=>{
  try{
    const res = await axios.get('/test1')
    return res.data
  }catch(error){
    console.log(error)
  }
}

0개의 댓글