황을 보고하는 기능이 있고, 나중에 언젠가는 이런 기능이 자바스크립트 프라미스에 도입될 수
도 있겠지만, 지금 당장은 그런 기능에 기대지 않고 다른 방법을 찾아야 합니다. 이벤트에 대해
알아봐야겠군요.
14.3.3
이벤트
이벤트는 자바스크립트에서 자주 사용됩니다. 이벤트의 개념은 간단합니다. 이벤트가 일어
나면 이벤트 발생을 담당하는 개체
emitter
에서 이벤트가 일어났음을 알립니다. 필요한 이벤트
는 모두 주시
listen
할 수 있습니다. 어떻게 이벤트를 주시할까요? 물론 콜백을 통해서입니다. 이
벤트 시스템을 직접 만드는 것도 별로 어려운 일은 아니지만, 노드에는 이미 이벤트를 지원하
는 모듈
EventEmitter
가 내장돼 있습니다.
1
이 모듈을 써서
countdown
함수를 개선해 봅시다.
EventEmitter
는
countdown
같은 함수와 함께 사용해도 되지만, 원래는 클래스와 함께 사용하
도록 설계됐습니다. 그러니 먼저
countdown
함수를
Countdown
클래스로 바꿔 봅시다.
const
EventEmitter
=
require
('
events
').
EventEmitter
;
class
Countdown
extends
EventEmitter
{
constructor
(
seconds
,
superstitious
) {
super ...
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.