Note that for some of the more common asynchronous API with timer support (eg. the select()
or kqueue()
system call) a timer value of zero (0
) represents the value of infinity so you cannot implement a function like setImmediate()
or nextTick()
the usual way by calculating the next timeout value and calling the asynchronous API. Instead all setImmediate()
or nextTick()
callbacks must be called synchronously before calling the asynchronous API.
Therefore, since C
is a callback to setImmediate()
it will be called before we re-enter the event loop to wait for B
to fire.
So if A
and only a gets triggered then the sequence of function calls would be: A
and C
.
But if A
and B
happens at the same time the sequence of function calls would be: A
, B
, C
.
Note that I said A
and C
and not A
, C
, B
because we didn't say B
gets triggered. For all I know B
will only get triggered 8 months from now or even never.
But if A
gets triggered and 1 millisecond later B
happens then the sequence of function calls would be: A
, C
, B
Is it possible for two events to happen in the same event loop? Generally yes. If it's network I/O it is unlikely because IP packets are serialized but it can still happen especially if you have a machine with multiple network cards. If it's graphical events like DOM updates then two things happening in the same event loop is quite common eg: both page resize and content reflow can happen at the same time especially with responsive CSS using media query where page resize can trigger content reflow.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…