기대대로 된다면 좋겠지만, 그것은 비동기적이지 않습니다. 비동기적 실행의 가장 큰 목적, 가
장 중요한 요점은
어떤 것도 차단하지 않는다
는 것입니다. 자바스크립트는 싱글 스레드를 사용
하므로, 우리가 컴퓨터에
60
초 동안 대기한 후 코드를 실행하라고 지시한다면, 그리고 그 실행
이 동기적으로 이루어진다면 꽤 골치 아픈 일이 벌어집니다. 프로그램이 멈추고, 사용자 입력
을 받아들이지 않고, 화면도 업데이트하지 않을 겁니다. 이런 일을 한 번씩은 겪어봤을 테고,
절대 유쾌한 경험은 아닐 겁니다. 비동기적 테크닉은 프로그램이 이런 식으로 멈추는 일을 막
아줍니다.
이 예제에서는 명확하게 표현하기 위해 이름 붙은 함수
f
를
setTimeout
에 넘겼습니다. 이름
붙은 함수를 써야 하는 타당한 이유가 없다면, 일반적으로는 다음과 같이 익명 함수를 사용합
니다.
setTimeout
(
function
() {
console
.
log
("
After
timeout
: " +
new
Date
());
},
60
*
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.