Skip to content

Commit f80d7cc

Browse files
fixed typo, added example as per @trevnorris
1 parent 35cf726 commit f80d7cc

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

doc/topics/the-event-loop-timers-and-nexttick.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,26 @@ completion.
261261
By placing it in a `process.nextTick()`, the script still has the ability to
262262
run to completion, allowing all the variables, functions, etc., to be
263263
initialized prior to the callback being called. It also has the advantage of
264-
not allowing the event loop_ to continue. It may be useful that the user be
264+
not allowing the event loop to continue. It may be useful that the user be
265265
alerted to an error before the event loop is allowed to continue.
266266

267+
A real world example in node would be:
268+
269+
```js
270+
const server = net.createServer(() => {}).listen(8080);
271+
272+
server.on('listening', () => {});
273+
```
274+
275+
When only a port is passed the port is bound immediately. So the `'listening'`
276+
callback could be called immediately. Problem is that the `.on
277+
('listening')` will
278+
not have been set by that time.
279+
280+
To get around this the `'listening'` event is queued in a `nextTick()` to allow
281+
the script to run to completion. Which allows the user to set any event
282+
handlers they want.
283+
267284
## process.nextTick() vs `setImmediate()`
268285

269286
We have two calls that are similar as far as users are concerned, but their

0 commit comments

Comments
 (0)