async 키워드는 함수 앞에 위치한다.
async function 함수명() {
await 비동기_처리_메서드_명();
}
함수 앞에 Async키워드를 두는 것은, '이 함수는 프로미스를 반환하겠다'는 의미이다. 결괏값은 자동으로 resolve()로 감싸진다.
// async & await 적용 후
async function 함수명() {
const response = await getData();
console.log(response);
}
Promise에서는 then과 catch를 사용했다.
async/await에서는 try catch를 사용하면 된다.
async function logTodoTitle() {
try {
const response = await getData();
console.log('## async await SUCCEED :', response);
} catch(e) {
console.error('## async await FAILED :', e);
}
}
run();