Skip to content
Merged
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
46f0702
doc: add topic - event loop, timers, `nextTick()`
techjeffharris Jan 28, 2016
5a28415
doc: add topic - event loop, timers, `nextTick()`
techjeffharris Jan 28, 2016
dc1b8a5
corrections suggested in the GitHub PR
techjeffharris Jan 29, 2016
bb5b682
Merge branch 'doc-topic-event-loop-timers-nextTick' of github.com:tec…
techjeffharris Jan 29, 2016
ba98380
removed file without .md extension
techjeffharris Jan 29, 2016
936bf17
add details to explanation of timers
techjeffharris Jan 29, 2016
35cf726
update to address comments on PR
techjeffharris Feb 17, 2016
f80d7cc
fixed typo, added example as per @trevnorris
techjeffharris Feb 24, 2016
254694b
fixed styling nits identified by @mscdex
techjeffharris Feb 24, 2016
45fb2fe
fixes suggested by @silverwind and @fishrock123
techjeffharris Feb 26, 2016
d6d76f5
addressed comments made on GH issue
techjeffharris Mar 25, 2016
c133caf
updated `setImmediate()` vs `setTimeout()` section
techjeffharris Mar 25, 2016
f425164
update overview, phase detail headings, wrap at 72
techjeffharris Mar 29, 2016
1bd3e6c
docs: minor nits on the libuv phases.
mcollina Mar 31, 2016
7574d4b
Removed second timer phase.
mcollina Mar 31, 2016
8dc6ecb
Merge pull request #1 from mcollina/doc-topic-event-loop-timers-nextTick
techjeffharris Mar 31, 2016
d82a7f1
fix nits presented by @ajafff
techjeffharris Mar 31, 2016
1dc26f6
fix backticks on line 205
techjeffharris Mar 31, 2016
82d0fb8
Improve wording `setTimeout()` vs `setImmediate()`
techjeffharris Apr 7, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions doc/topics/the-event-loop-timers-and-nexttick.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ order of operations.
┌─>│ timers │
│ └──────────┬────────────┘
│ ┌──────────┴────────────┐
│ │ i/o callbacks │
│ │ I/O callbacks │
│ └──────────┬────────────┘
│ ┌──────────┴────────────┐
│ │ idle, prepare │
Expand All @@ -38,10 +38,7 @@ order of operations.
│ │ check │
│ └──────────┬────────────┘
│ ┌──────────┴────────────┐
│ │ close callbacks │
│ └──────────┬────────────┘
│ ┌──────────┴────────────┐
└──┤ timers │
└──┤ close callbacks │
└───────────────────────┘

*note: each box will be referred to as a "phase" of the event loop.*
Expand Down Expand Up @@ -72,12 +69,15 @@ actually uses are these four._

* `timers`: this phase executes callbacks scheduled by `setTimeout()`
and `setInterval()`.
* `i/o callbacks`: most types of callback except timers, setImmedate, close
* `I/O callbacks`: most types of callback except timers, setImmedate, close
* `idle, prepare`: only used internally
* `poll`: retrieve new i/o events; node will block here when appropriate
* `poll`: retrieve new I/O events; node will block here when appropriate
* `check`: setImmediate callbacks are invoked here
* `close callbacks`: e.g socket.on('close', ...)
* `timers`: indeed, again

Between each run of the event loop, Node.js checks if it is waiting for
any asynchronous I/O or timer and it shuts down cleanly if there are not
any.

## Phases in Detail

Expand Down Expand Up @@ -147,12 +147,12 @@ Note: To prevent the `poll` phase from starving the event loop, libuv
also has a hard maximum (system dependent) before it stops `poll`ing for
more events.

### i/o callbakcs:
### I/O callbacks:

This phase executes callbacks for some system operations such as types
of TCP errors. For example if a TCP socket receives `ECONNREFUSED` when
attempting to connect, some \*nix systems want to wait to report the
error. This will be queued to execute in the `i/o callbakcs` phase.
error. This will be queued to execute in the `I/O callbacks` phase.

### poll:

Expand Down Expand Up @@ -202,13 +202,11 @@ etc. However, after a callback has been scheduled with `setImmediate()`,
then the `poll` phase becomes idle, it will end and continue to the
`check` phase rather than waiting for `poll` events.

### `close callbacks:



### `timers` again

### `close` callbacks:

If a socket or handle is closed abruptly (e.g. `socket.destroy()`), the
`'close'` event will be emitted in this phase. Otherwise it will be
emitted via `process.nextTick()`.

## `setImmediate()` vs `setTimeout()`

Expand Down Expand Up @@ -271,9 +269,9 @@ fs.readFile(__filename, () => {
immediate
timeout

The main advantage to using `setImmediate()` over `setTimeout()` is that
`setImmediate()` has high probability of running before setTimeout when
both are called within the same I/O cycle.
The main advantage to using `setImmediate()` over `setTimeout()` is
`setImmediate()` will always be executed before any timers if scheduled
within an I/O cycle, independently on how many timers are present.

## `process.nextTick()`:

Expand Down