📎아이템 25 비동기 코드에는 콜백 대신 async 함수 사용하기
📍 콜백
declare function fetchURL(
url: string, callback: (response: string) => void
): void;
fetchURL(url1, function(response1) {
fetchURL(url2, function(response2) {
fetchURL(url3, function(response3) {
// ...
console.log(1);
});
console.log(2);
});
console.log(3);
});
console.log(4);
// Logs:
// 4
// 3
// 2
// 1📍 프로미스(promise)
📍 async, await
✓ 콜백보다 async/await를 사용해야 하는 이유
🔗 병렬로 페이지를 로드하고 싶은 경우
🔗 입력된 프로미스들 중 첫 번째가 처리될 때 완료되는 Promise.race
🔗 프로미스를 직접 생성해야 할 때
📍 요약
Last updated