종우의 컴퓨터 공간
★Async & Await 본문
Async & Await 이란?
- async와 await은 자바스크립트의 비동기 처리 패턴 중 가장 최근에 나온 문법이다. 기존의 비동기 처리 방식인 콜백 함수와 프로미스의 단점을 보완하고 개발자가 읽기 좋은 코드를 작성할 수 있게 도와준다.
특징
- await은 async 안에서만 사용 가능하다.
- async 함수는 프로미스를 반환한다.
- await은 resolve로 리턴해주는 값을 꺼낸다.
예제
async function getUsers() {
// await response of the fetch call
const response = await fetch('https://jsonplaceholder.typicode.com/users');
// Only proceed once its resolved
const data = await response.json();
// only proceed once second promise is resolved
return data;
}
getUsers()
.then(users => console.log(users));
'자바 스크립트 (JavaScript) > Asynchronous JS, Ajax & Fetch API' 카테고리의 다른 글
Custom HTTP Library (Fetch with Async Await) (0) | 2021.08.17 |
---|---|
[ES6]Custom HTTP Library (Fetch with Promises) (0) | 2021.08.16 |
★[ES6]Arrow Functions (0) | 2021.08.13 |
★[ES6]Fetch API (0) | 2021.08.13 |
★[ES6] Promises (0) | 2021.08.11 |