nodejs.dev icon indicating copy to clipboard operation
nodejs.dev copied to clipboard

"Learn" docs on setTimeout(foo, 0) vs. API's minimum delay 1

Open joshkel opened this issue 4 years ago • 4 comments
trafficstars

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.

joshkel avatar Oct 18 '21 22:10 joshkel

Can I work on this issue?

kailash360 avatar Nov 10 '21 18:11 kailash360

@kailash360 , I'm not associated with the project, but based on CONTRIBUTING.md, it sounds like a PR would be welcome.

joshkel avatar Nov 30 '21 02:11 joshkel

const start = async () => {
  const timeout = await setTimeout(() => {}, 0);
  console.log(timeout._idleTimeout);
};

start();

image

it will output 1 if we run on the NodeJs environment.

MrJithil avatar Jan 09 '22 17:01 MrJithil

@kailash360, if you're still interested in working on this issue, feel free.

ovflowd avatar Jul 27 '22 19:07 ovflowd

Closing because these pages have gone away. https://github.com/nodejs/nodejs.dev/pull/2796

benhalverson avatar Sep 30 '22 01:09 benhalverson