-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doc: Adding guide on Timers #6825
Changes from 4 commits
1c0f9ad
c73869c
e419e50
291d1aa
deec87d
5c16d99
5475e00
011a95e
80ec978
103e566
020b941
20c55c0
0fb1eeb
14a644f
9377403
2f78f5f
f7fa1a9
afa5167
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
--- | ||
title: Timers in node.js | ||
layout: docs.hbs | ||
--- | ||
|
||
# Timers in node.js and beyond | ||
|
||
Timers are a collection of global functions in node.js that allow you to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better if we mention "Node.js" consistently. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/node.js/Node.js There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep in mind you can actually do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reworking this and fixing capitalization. |
||
execute code after a set period of time. To fully understand when timer | ||
functions will be executed, it's a good idea to read up on the the node | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/node/Node.js (here and everywhere else it's mentioned ;-) ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ty |
||
[Event Loop](https://nodesource.com/blog/understanding-the-nodejs-event-loop/). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should link to https://github.com/nodejs/node/blob/master/doc/topics/the-event-loop-timers-and-nexttick.md (using a local link), which is an updated and revised version of that blog post. |
||
|
||
## Code Time Machine | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems disconnected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Going to rework this a bit to provide a better introduction to the |
||
|
||
The node API provides several ways to schedule your code to execute at | ||
some point after the present moment. | ||
|
||
### "When I say so" Execution ~ *setTimeout()* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. *`setTimeout()`* |
||
|
||
Using `setTimeout()`, you can schedule code to execute after a designated | ||
amount of milliseconds. You may already be familiar with this function, as | ||
it is a part of V8 and thus part of the browser JavaScript API. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I am recalling correctly, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Is it really? ;) I'm sure many people don't know (and that is ok! It means we have done a good job), but we actually need to have our own timers implementation. Browsers provide that, not the JS Engine. I'll add an extra note in the main comment thread here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! I'll fix this. |
||
|
||
`setTimeout()` accepts the code to execute as its first argument and the | ||
millisecond delay defined as a number literal as the second argument. Here | ||
is an example of that: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted in the docs, you can also pass any number of arguments though to the function being called via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added explanation and revised the example to demo this as well. |
||
|
||
```js | ||
function myFunc () { | ||
console.log('funky'); | ||
} | ||
|
||
setTimeout(myFunc, 1500); | ||
``` | ||
|
||
The above function `myFunc()` will execute after approximately 1500 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
milliseconds (or 1.5 seconds) due to the call of `setTimeout()`. | ||
|
||
The timeout interval that is set cannot be relied upon to execute after | ||
that *exact* number of milliseconds. This is because executing code that | ||
blocks or holds onto the event loop will push the execution of your timeout | ||
back. You *can* guarantee that your timeout will not execute *sooner* than | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. timeout function will not execute? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. meaning, it won't execute sooner than the timeout, it will execute on or after the timeout. |
||
the declared timeout. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was not actually always true on windows, but I think that was fixed now. Should be fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. didn't know that! thanks! |
||
|
||
### "Right after this" Execution ~ *setImmediate()* | ||
|
||
`setImmediate()` allows you to execute code at the beginning of the next | ||
event loop cycle. This code will execute *before* any timers or IO operations. | ||
I like to think of this code execution as happening "right after this", meaning | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sounds like personal opinion rather than the actual fact. Can you please reword this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally should avoid language such as "I like" or "allows you", etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm... the "I like to think" line is a way of injecting a little personality into it, breaking down the order of when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docs can't contain lines like "I like to think", it should read as Node's docs and not someone's blog post about timers. Perhaps "A good mental model of setImmediate is..." |
||
any code following the `setImmediate()` function call will execute before the | ||
`setImmediate()` function argument. Here's an example: | ||
|
||
```js | ||
let order = 'before immediate\n'; | ||
|
||
setImmediate(() => { | ||
order += 'executing immediate\n'; | ||
}); | ||
|
||
order += 'after immediate\n'; | ||
|
||
console.log(order); | ||
``` | ||
|
||
The above function passed to `setImmediate()` will execute after all runnable | ||
code has executed, and the console output will be: | ||
|
||
```shell | ||
before immediate | ||
after immediate | ||
executing immediate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're right. not sure how that snuck in. :-S |
||
``` | ||
|
||
Note: `process.nextTick()` is very similar to `setImmediate()`. The two major | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really. The primary difference is that immediates allows IO to run before they are called, whereas nextTick does not. (nextTick is also not safe to call recursively in a potentially unbounded (infinite) way.) |
||
differences are that `process.nextTick()` will run *before* any immediates that | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. immediates is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This, perhaps, is a bit too simplistic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nah, but I think there should be a prelude that explains that "timeouts" ( |
||
are set. The second is that `process.nextTick()` is non-clearable, meaning once | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to put it something like (roughly) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Going to rework this whole paragraph to better illustrate the differences with |
||
you've scheduled code to execute with `process.nextTick()` you cannot stop that | ||
code, unlike with `setImmediate()`. | ||
|
||
### "Deja vu" Execution ~ *setInterval()* | ||
|
||
If there is a block of code that you want to execute multiple times, you can | ||
use `setInterval()` to execute that code. `setInterval()` takes a function | ||
argument that will run and infinite number of times with a given millisecond | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and is not needed here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ty |
||
delay. Just like `setTimeout()`, the delay cannot be guaranteed because of | ||
operations that may hold on to the event loop, and therefore should be treated | ||
as an approximate delay. See the below example: | ||
|
||
```js | ||
function intervalFunc () { | ||
console.log('Cant stop me now!'); | ||
} | ||
|
||
setInterval(intervalFunc, 1500); | ||
``` | ||
In the above example, `intervalFunc()` will execute every 1500 milliseconds, or | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it |
||
1.5 seconds, until it is stopped (see below). | ||
|
||
## Master of the Timerverse | ||
|
||
What would a Code Time Machine be without the ability to turn it off? | ||
`setTimeout()`, `setImmediate()`, and `setInterval()` return a timer object | ||
that can be used to reference the set timeout, immediate, or interval object. | ||
By passing said objective into the respective `clear` function, execution of | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. said object There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks |
||
that object will be halted completely. The respective functions are | ||
`clearTimeout()`, `clearImmediate()`, and `clearInterval()`. See the example | ||
below for an example of each: | ||
|
||
```js | ||
let timeoutObj = setTimeout(() => { | ||
console.log('timeout beyond time'); | ||
}, 1500); | ||
|
||
let immediateObj = setImmediate(() => { | ||
console.log('immediately executing immediate'); | ||
}); | ||
|
||
let intervalObj = setInterval(() => { | ||
console.log('interviewing the interval'); | ||
}, 500); | ||
|
||
clearTimeout(timeoutObj); | ||
clearImmediate(immediateObj); | ||
clearInterval(intervalObj); | ||
``` | ||
|
||
## Last Train to Nowhere | ||
|
||
The node Timer API provides two functions intended to augment timer behavior | ||
with `unref()` and `ref()`. If you have a timer object scheduled using a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should specify that these are on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good distinction, I'll add this in. |
||
`set` function, you can call `unref()` on that object. This will change | ||
the behavior slightly, and not call the timer object *if it is the last | ||
code to execute*. Instead, it will let the program exit cleanly. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better stated as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ty |
||
|
||
In similar fashion, a timer object that has had `unref()` called on it | ||
can remove that behavior by calling `ref()` on that same timer object, | ||
which will then ensure its execution. See below for examples of both: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
```js | ||
let timerObj = setTimeout(() => { | ||
console.log('will i run?'); | ||
}); | ||
|
||
// if left alone, this statement will keep the above | ||
// timeout from running, since the timeout will be the only | ||
// thing keeping the program from exiting | ||
timerObj.unref(); | ||
|
||
// we can bring it back to life by calling ref() inside | ||
// an immediate | ||
setImmediate(() => { | ||
timerObj.ref(); | ||
}); | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually we capitalize "Node.js", or otherwise use just "node".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it. thanks