Skip to content
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

Why does the function setTimeout() return the different value between nodejs and chrome console? #1903

Closed
bahuwang opened this issue May 9, 2019 · 2 comments

Comments

@bahuwang
Copy link

commented May 9, 2019

  • Node.js Version: 10.15.3
  • OS: windows 10 1809
  • Scope (install, code, runtime, meta, other?): code
  • Module (and version) (if relevant): Timers
    var a = setTimeout(function () {
        console.log('time')
    },1000 )
    console.log(a)

I want to know why the return value of nodejs is Timeout and chrome console is a timeoutID.

@bahuwang bahuwang changed the title why does the function setTimeout() return the different value between nodejs and chrome console? Why does the function setTimeout() return the different value between nodejs and chrome console? May 9, 2019

@gireeshpunathil

This comment has been minimized.

Copy link
Member

commented May 10, 2019

as per docs: https://nodejs.org/dist/latest-v12.x/docs/api/timers.html

The timer functions within Node.js implement a similar API as the timers API provided by Web Browsers but use a different internal implementation that is built around the Node.js Event Loop.

According to me, the timer functions in Node are implemented using the same type of abstraction that other asynchronous APIs are designed with, leveraging the register- poll / check - trigger mechanism that the event loop uses. The major difference is that instead of polling the OS, it detects the scheduled intervals with the help of an internal counter that the loop maintains. Otherwise, more or less the same abstractions - native bindings, request registration, triggering callbacks etc. remain more or less same.

Having said that, the scheduling (set..) and de-scheduling (clear..) semantics do not impose any difference at the API level, to the user - between browsers and node, so!

Hope this explanation helps?

@bahuwang

This comment has been minimized.

Copy link
Author

commented May 13, 2019

Thanks @gireeshpunathil. Maybe based on different considerations, there are some inconsistents.

@bahuwang bahuwang closed this May 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.