►
From YouTube: Morning Keynote- Everything You Need to Know About Node.js Event Loop - Bert Belder, IBM
Description
Morning Keynote- Everything You Need to Know About Node.js Event Loop - Bert Belder, IBM
If you're a Node.js programmer, you probably know that node has an event loop, just like a web browser. But there are many misconceptions about how it really works. Does Node use threads, or not? Where do callbacks come from? Is it better to use nextTick or setImmediate? Node's event loop is built on libuv, and libuv's author is here to tell you all a Node developer needs to know about the event loop.
A
Good
morning
who
you
more
I
can
I
can
speak
Dutch,
Dale
good,
all
right
well
for
people
that
don't
know
me
you're
not
supposed
to
know
me
anyway.
A
So
you
know
tune
in
to
your
subconsciousness,
for
you
know
invisible
instances
of
product
placement
and
like
things
you
really
do
not
want
to
know.
Do
you
know
that?
Does
anyone
here
has
like
the
someone
on
the
Internet's
wrong
syndrome?
Sometimes
so
like
this?
Usually,
when
you
do
it
keynote,
you
have
to
talk
about
like
the
community
and
like
where
it's
very
important
by
the
way,
but
and
like
you
know,
where's
the
project
heading
like
in
the
grand
scheme
of
things,
but
this
is
not
this
talk.
A
This
is
just
purely
a
technical
talk
and
the
reason
is
that
if
sometimes
I
entered
this
in
the
you
know,
you
want
to
make
it
talk
to
yourself
and
you
enter
no
Jesse
venting
for
some
sort
of
diagram
and
every
time
I
cringe
because,
like
these
are
all
wrong.
I'm,
sorry
like
if
you
made
one
of
these
I'll,
buy
your
beer
afterwards,
but
like
no.
No,
no
so
like,
for
example,
do
does
like
networking
happening
and
happen
in
the
threat
pool.
No,
not
really.
No
and
the
event
queue
is
not
another
thing.
A
What
was
this
slide
again?
Oh,
yes,
the
event
loop
returns
stuff
to
decline.
No,
it
does
not.
There's
no
non-blocking
worker
thread
pool
now-now-now
or
you
know
a
stack
of
events.
You
know
you
know
stack
is
a
data
structure
and
you
take,
you
know,
put
stuff
on
top
and
you
take
them
top
first.
No!
It's
not
a
thing,
but
the
photo
by
the
way
is
it's.
Trevor
Norris
I
did
not
want
to
implicate
him.
He's
an
awesome.
A
Ctc
member,
it
was
just
a
coincident.
This
is
a
this
one's
kind
of
good.
It
I
have
some.
You
find
this
on
Stack
Overflow,
it's
probably
the
best
one
that
you
can
find
on
the
internet,
but
still
I
do
not
agree
with
the
way
process.
The
next
stick
is
added
to
this
slide.
Okay,
so,
first
before
I,
get
started
with
like
I,
showing
how
you
how
it
should
be
represented.
I
need
to
share
some
dirty
secret
with
you,
so
we're
kind
of
used
to
well,
if
you're
not
doing
no
js'
and
like
asynchronous
I/o.
A
You've
ended
programming,
you're
kind
of
used
to
writing
code
like
this
right,
like
you,
say,
connect
and
write
and
read,
and
you
make
you
give
commands
to
your
operating
system
and
will
do
stuff
for
you,
and
you
know
you
said
what
you
want.
It
will
give
you
the
result
back,
and
you
know
for
a
you
know
the
new
person.
It
might
seem
that
your
computer
is
actually
working
really
hard
when
you're
doing
this.
A
When
you're
you
know
sending
data
to
the
network,
it
might
be,
you
know
busy
with
it,
but
really
that's
total,
like
somewhere
in
the
in
the
early
90s,
every
computer
learned
a
trick
called
direct
memory
access,
and
that
meant
that
you
know
whenever
your
CPU
fetches
your
program
runs
on
your
CPU
right.
I
wanted
to
do
something
with
you
know
hard
disk
or
network
card.
A
Have
a
very
you
know:
questionable
attention
span,
because
at
the
moment
that
happens,
it
will
just
instantly
jump
and
you
know,
go
handle
that
interrupt
and
kind
of
forget
where
it
was
in
the
first
place,
it's
kind
of
an
issue.
It's
very
hard
to
program
computers
like
that,
if
you
you
know
have
done
some
microcontroller
programming
I'd
have
to
do
that
yourself.
It's
really
not
easy
to
get
right,
so
it's
kind
of
understandable
that
people
came
up
this
because
of
just
you
know
if
you're
not
very
smart.
A
Like
me,
this
is
just
way
easier,
oh
yeah,
so
this
is
the.
This
is
the
dirty
secret.
Your
operating
system
takes
care
of
like
that.
The
total
lie
that
you're
dealing
with
anyway
I
know
Jess,
we,
you
know
we
have
an
event
loop,
and
that
means
that
we
can
do
asynchronous
programming,
but
we
don't
have
to
you
know,
deal
with
the
like
a
DeeDee
kind
of
a
processor,
the
way
the
processor
works.
So
this
is
the
start.
This
is
how
I
would
start
in
a
diagram
of
an
event
loop.
You
start
with.
A
You
know
it
always
starts
with
your
actual
program.
It's
not
a
callback,
it's
just
whatever
is
in
index
J
s
or
whatever
happens
when
you
do
NPM
start
and
at
the
very
end
we
also
know.
There's
you
always
get
this
like
process
exit
event
and
and
if
everyone
who
has
ever
tried
to
use
it
knows
that
you
know
you
can't
really
use
it
for
anything,
because
it's
really
the
last
thing
that's
ever
going
to
happen
and
since
no
Jess
asynchronous
you
know
you
set
a
timer,
it
will
never
fire
you
right.
A
You
know
you
try
to
write
some
data
to
the
network,
it
might
go
out,
but
you
can
never
tell
it's
really,
not
very
practical,
but
this
is
sort
of
the
beginning
in
the
end.
So,
let's
fill
out
the
blanks
there's
just
basically
four
steps
that
an
event
loop
takes
at
every
every
iteration.
First
of
it
is,
it
will
figure
out
if
there
were
any
timeout.
A
You
know
so
I'll
get
into
how
that
actually
works
later
then,
there's
the
unicorn
and
unicorn
I'll.
Give
you
much
more
detail
on
that
later,
but
that
does
the
most
of
the
magic.
If
there's
any
disk
or
network
or
child
process
activity
going
on
in
your
process,
it
will
find
those
events
there
and
it
will
maybe
do
some
more
management
internally
and
it
will
call
your
callbacks,
that's
the
most
important
things.
Think
and
I
have
all
these
like
for
Jas
boxes
in
here,
because
there's
really,
no,
you
know
event
loop.
A
That
sends
things
like
it's
it.
You
know
your
event.
Loop
just
goes
back
and
forth
it
you
know
into
it,
goes
into
libya
v
and
fingers
timers
and
ankles
your
javascript,
and
then
it
figures
out
network
events
and
calls
your
javascript
and
then
it
goes
on
in
set
immediate
queue.
So
that's
everything
you
did
with
set
immediate,
and
at
that
point
it
will
call
your
callbacks
and
then
finally
there's
this
sort
of
internal
phase
where
we
create
closed
events
and
clean
up
open,
sockets
and
that
kind
of
stuff.
A
And
then
we
get
a
dislike
nice
little
corner
here
and
you
know,
leave
UV
or
no
decides
whether
to
keep
running
or
go
to
that
like
infamous
process
exit
event.
Alright,
so
a
little
bit
more,
you
know
a
detail
here.
Let's
say
your
little
JavaScript
that
run
right
there
after
figuring
this
set
immediate
callbacks,
you
know
is,
let's
say
it
calls
set
timeout.
What
what
happens
internally?
Is
we
add
your?
You
know
your
timer,
we
compute
when
you
know
when
to
set
the
alarm.
Basically
we
add
it
to
the
timer
heap.
A
This
is
a
picture
of
like
there's
an
actual
timer
heap
somewhere
in
the
world
you're
from
friends.
You
might
recognize
it.
This
is
at
sound
Lazare
station
and
at
the
by
the
time
the
event
loop
gets
to
the
little
alarm
spot.
It
will
look
at
the
at
the
timer
heap
figure
out,
which
ones
have
expired
and
line.
Call
your
callback
simple
enough
same
goes
for,
let's
say
creating
a
server.
A
The
server
is
not
done
with
the
thread
pool
it's
we
go
directly
to
the
operating
system
and
ask
it
hey
give
us,
you
know,
give
us
a
notification
if
a
new
connection
is
made
to
our
server
and
then
you
know
when
the
unicorn
function
is
trying
to
trying
to
figure
out
those
events,
it
will
pick
up
the
fact
that
there
is
a
new
connection.
Last
but
not
least,
the
minions,
we
call
it
workers
internally
worker
threads,
there's.
A
Typically,
four,
not
three,
and
you
know:
if
you
do
something
like
the
file
system,
it
will
get
a
worker,
it
will
go,
do
it
for
you
and
then
at
again
that
the
unicorn
function
will
figure
that
out.
This
also
kind
of
explains
how
no
just
knows
how
either
it
should
exit
or
it
should
just
keep
going,
because
it's
it
simple.
As
that
you
know
every
time
we
send
you
know,
we
start
an
operation
like
this
file,
stuff
that
we
send
to
the
worker.
A
We
add,
add
one
to
the
reference
counter
and
then,
when
the
call
back
comes
when
the
event
happens,
we
just
subtract
one
very
simple,
and
that
means
that
at
that,
like
little,
you
know
split
where
we
can
either
decide
exit
or
continue.
We
can
just
look
if
the
reference
counter
is
zero,
we
exit,
otherwise
we
don't
simple
as
that.
Okay,
let
me
tell
you
a
little
bit
about
the
Unicorn
function.
Oh
this.
Is
that
thing
again
the
the
code
that
I
showed
you
in
the
beginning.
A
What
stands
out
about
these
functions
can
act
right
and
read
is
that
they
both
do
something
and
they
get
the
result.
That
means
that,
at
you
know,
your
program
of
course
cannot
keep
running
and
what
we
want
to
do
inside
nodejs
is
kind
of
kick
off
these
operations,
but
we
know
it
cannot,
because
we
want
to
do
some
things
asynchronously.
A
We
cannot
have
to
result
immediately,
and
so
what
the
Unicorn
function
must
be
able
to
do
is
get
the
get
the
results
of,
like
anything
that
was
going
on
in
the
background
I'm
not
going
to
go
into
all
the
details
of
how
this
works.
Actually,
if
you
want
to
do
that,
I
think
there's
a
code
and
learn
on
Saturday
morning
by
Sawa,
and
he
will
give
you
probably
all
the
details
and
stuff
you
even
don't
want
to
know.
A
A
You
know
reads
that
one
at
the
other
side,
k
event
is
what
what
happens
in
BS,
DS
and
and
OS
x,
kind
of
the
same
thing,
and
then
there's
windows.
It
has
get
queued,
completion
status
and,
as
you
can
already
see
in
the
name,
it
gives
you
a
completion
status
that
was
somehow
cute
and
the
completion
status.
Of
course,
the
result
that
we
were
looking
for
and
that
queuing
is
happening
actually
automatically.
And
then
what
I'm
not
saying
here
seeing
here
is
there
is
all
these
unicorn
functions
must
have
a
timeout.
A
So
when
we
run
them,
what
we
do
is
we
basically
compute
when
the
next
timeout?
You
know
it
will
happen,
we
don't
really
care
about
the
ones
in
the
far
future,
just
the
next
one.
So
we
can,
you
know,
say:
okay,
if
nothing
happens
for
the
next
ten
seconds
and
the
next
time
it's
in
the
next
ten
seconds,
then
it
will
just
return
with
no
events
and
that
we,
you
know
the
event
loop
will,
you
know,
go
back
spinning
and
it
will
hit
that
timer
place
again
and
run
your
time
out.
A
There
was
something
else
I
needed
to
say
about
this,
but
I
forgot
that's
kind
of
dumb
alright.
So
if
you
need
to
make
any
diagrams,
just
don't
pretend
everything
happens
in
a
threat.
Well,
that's
very
painful
for
me
because
we
tried
very
hard
not
to
do
that
and
also
it
confuses
people
because
they're
like
well
notice,
like
really
not
like
you
know,
they
say
synchronous
with
research
flat
poo
is
just
like
Java.
That's
not
true.
What
we
cared
about
most
when
we
made
no
js'
is
to
make
a
very
scalable
server.
A
So
something
that
could
you
like
thousands
of
open
connections,
and
you
can
actually
do
that
with
WebSockets.
If
you
ever
tried
that
no
just
doesn't
break
a
sweat,
and
so
whenever
we
can,
we
we
try
to
use
the
kernel.
So
nothing
else.
We
just
ask
the
operating
system.
Hey,
gives
a
signal
and
tell
the
unicorn
function
when
you
know
when
there's
when
something
happened,
but
that's
it's
not
always
possible.
Unfortunately,
operating
systems
are
not
very
good
and
so
files
and
sometimes
pipes,
have
to
run
in
the
thread
pool
and
then
there's
this.
A
This
is
very,
maybe
practical
to
know
whenever
you
look
up
a
domain,
so
you
say:
well,
you
use
a
request.
For
example,
you
say:
hey
get
me
google.com
and
what
happens
is
the
DNS
lookup
function
gets
called,
and
that
also
runs
in
a
thread
pool
and
the
reason
for
that
is
that
the
way
your
computer
looks
up,
DNS
locates,
the
NS
records
is
kind
of
operating
system.
Specific
and
people
are
expected
to
accept
to
happen
or
to
behave
exactly
the
same
as
your
browser,
and
so
we
have
to
use
the
operating
system
there.
A
There
are
better
DNS
functions
that
actually
actually
are
asynchronous,
but
you
have
to
call
them
manually,
and
but
if
you
ever
have
a
performance
issue,
I
would
say
try
to
look
at
that.
The
bottom
two
rows
are
not
really
or
combs
are
not
really
that
important.
We
need
to
use
single
hand,
hinder,
sometimes
signal
handlers
are
kind
of
like
interrupts
in
the
in
the
sort
of
computer
architecture
picture.
It
is
also
something
that
can
immediately
distract
your
program.
A
That's
why
nobody
ever
uses
signal
handlers
for
any
serious
programming,
because
it's
kind
of
impossible
to
get
right
and
windows
has
like
this
weird,
weird
concept
of
wait:
threats,
which
means
you
spin
up
a
threat
to
basically
just
wait
for
stuff
and
we
needed
to
use
it.
Sometimes
we
try
not
to
so
to
complete
the
picture.
What
and
it
stands
out
about
this
if
you
compare
it
with
the
earlier
ones,
that
I
said
were
wrong
is
like
some
stuff
is
really
not
in
here.
A
A
Are
there
more
next
stick
events
or
callbacks
going
on
in
your
program,
and
it
will
also
keep
doing
that
until
there
are
no
more
next
a
callback.
So
if
you
remember
nodejs
or
10
I
believe
it
might,
it
would
sometimes
complain.
Hey
you
had
10,000,
that's
because
we
were
afraid
you
were
locking
up
your
program.
A
You
know
by
calling
next
it
continuously
and
it
would
never
escape
this
like
little
inside
loop
yeah.
Also
by
the
way.
It
isn't
it
weird
sorry
for
that
I'm
kind
of
responsible
next
stick
is
really
not
the
next
stick.
It's
just
immediate
and
then
said
you
me
is
not
very
immediate
at
all.
It's
more
like
the
next
stick,
but
whatever
just
remember
that,
okay,
this
is
all
I'll
tweet,
the
I'll
tweet,
the
the
slides.