nodejs.dev
nodejs.dev copied to clipboard
"Learn" docs on setTimeout(foo, 0) vs. API's minimum delay 1
Documentation Is
- [ ] Missing/Needed
- [X] Incorrect
- [ ] Confusing
- [ ] Not Sure?
Please Explain in Detail
The Node.js Learn pages have several discussions about setTimeout(foo, 0):
- https://nodejs.dev/learn/the-nodejs-event-loop
- https://nodejs.dev/learn/understanding-process-nexttick
- https://nodejs.dev/learn/understanding-setimmediate
- https://nodejs.dev/learn/discover-javascript-timers
In each case, it describes setTimeout(foo, 0) as executing immediately, similar to setImmediate(foo). However, according to the API documentation (and from my own testing), Node.js's setTimeout has a minimum delay of 1. From what I can tell, this is a difference between browser and Node.js environments; browsers are documented as supporting a timeout of 0.
Your Proposal for Changes
Replace some references to setTimeout(foo, 0) with setImmediate(foo). Point out the difference in minimum interval compared to the browser.
Can I work on this issue?
@kailash360 , I'm not associated with the project, but based on CONTRIBUTING.md, it sounds like a PR would be welcome.
const start = async () => {
const timeout = await setTimeout(() => {}, 0);
console.log(timeout._idleTimeout);
};
start();

it will output 1 if we run on the NodeJs environment.
@kailash360, if you're still interested in working on this issue, feel free.
Closing because these pages have gone away. https://github.com/nodejs/nodejs.dev/pull/2796