스는 콜백을 예측 가능한 패턴으로 사용할 수 있게 하며, 프라미스 없이 콜백만 사용했을 때 나
타날 수 있는 엉뚱한 현상이나 찾기 힘든 버그를 상당수 해결합니다.
프라미스의 기본 개념은 간단합니다. 프라미스 기반 비동기적 함수를 호출하면 그 함수는
Promise
인스턴스를 반환합니다. 프라미스는
성공
fulfilled
하거나,
실패
rejected
하거나 단 두 가지뿐
입니다. 프라미스는 성공 혹은 실패 둘 중
하나만
일어난다고 확신할 수 있습니다. 성공한 프라
미스가 나중에 실패하는 일 같은 건 절대 없습니다. 또한, 성공이든 실패든
단 한 번만
일어납
니다. 프라미스가 성공하거나 실패하면 그 프라미스를
결정됐다
settled
고 합니다.
프라미스는 객체이므로 어디든 전달할 수 있다는 점도 콜백에 비해 간편한 장점입니다. 비동기
적 처리를 여기서 하지 않고 다른 함수에서(또는 다른 동료가) 처리하게 하고 싶다면 프라미스
를 넘기기만 하면 됩니다. 마치 음식점에서 받은 예약 호출기를 친구에게 맡기는 것과 비슷합
니다. 예약한 인원이 때맞춰 오기만 한다면, 음식점에서는 누가 호출기를 들고 있든 상관없으
니까요.
14.3.1
프라미스 만들기
프라미스는 쉽게 만들 수 있습니다.
resolve
(성공)와
reject
(실패) 콜백이 있는 함수로 새
Promise ...
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.