async/await

SSAD·2023년 2월 14일
0

BackEnd

목록 보기
14/44

axios는 기본적으로 async/await를 지원하는 라이브러리

async function getData(url){
  let data = (await axios.get(url)).data;
	return data;
  }

let url = 'https://koreanjson.com/posts/1';

async function dataArr(){
    let data = await getData(url);
    let result = [];
    for(let v of Object.values(data)){
        result.push(v);
    }
    console.log(result);
}

dataArr();
  • 비동기 작업을 수행하고자 하는 함수 앞에 async를 표기
  • 실질적인 비동기 작업이 필요한 위치마다 await를 표기
  • 뒤의 내용을 자동으로 Promise로 자동 전환하고 해당 내용이 resolve된 이후에 다음으로 진행
profile
learn !

0개의 댓글