이대로라면 별로 좋은 함수는 아닙니다. 너무 장황한 데다가, 콘솔을 아예 쓰지 않기를 원할 수
도 있습니다. 웹 페이지에서 카운트다운이 끝나면 페이지 요소를 업데이트하는 목적에 쓰기도
별로 알맞지 않아 보입니다. 하지만 이제 시작일 뿐이고, 프라미스를 어떻게 만드는지는 잘 드
러나 있습니다.
resolve
와
reject
는 함수입니다.
resolve
를 여러 번 호출하면 프라미스의 프
라미스 같은 걸 만들 수 있지 않을까 하는 생각이 들 수도 있겠지만,
resolve
나
reject
를 여러
번 호출하든, 섞어서 호출하든 결과는 같습니다. 첫 번째로 호출한 것만 의미가 있습니다. 프라
미스는 성공 또는 실패를 나타낼 뿐입니다(현재 이 함수에는 실패 부분이 없습니다만).
14.3.2
프라미스 사용
countdown
함수를 어떻게 사용하는지 알아봅시다. 프라미스는 무시해버리고
countdown
(
5
)
처
럼 호출해도 됩니다. 카운트다운은 여전히 동작하고, 무슨 말인지 알기 어려운 프라미스는 신
경쓰지 않아도 됩니다. 하지만 프라미스의 장점을 이용하고 싶다면 어떻게 해야 할까요? 반환
된 프라미스를 사용하는 예제를 살펴봅시다.
countdown
(
5
).
then
(
function
() {
console
.
log
("
countdown
completed ...
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.