2

I have a piece of code that uses the setTimeout method as such

setTimeout(() => {
 console.log('Hello');
},1000);

For the duration of the 100ms or 1s where is the setTimeout method? Has it been passed to the system kernel until the callback is ready to be executed or has the V8 passed it to another Queue?

4 Answers 4

2

Source code for node's Timers is here and here.

setTimeout will create a new Timeout object which will get appended to a linkedlist itself added to a timerListMap object, which is just a plain JS Object where keys represent the duration in ms and values are the linkedlists.
Finally, this timerListMap object is queued in the timerListQueue PriorityQueue which once again is just a JS class instance.

Then when the event loop (libuv) will call processTimers(now) with a now value larger than the passed timeout, the callback will get retrieved following the reverse route.

So this all means that, in node, the callbacks to timeouts stay in the JS heap the whole time, the whole Timers logic stays in the JavaScript layer and only the asynchronous peeking is initialized by libuv event loop.

Sign up to request clarification or add additional context in comments.

Comments

1
() => {
 console.log('Hello');
}

This is the function that is executed after the timeout interval.

Based on the setTimeout method you have written, it waits for a minimum of 1000ms, then executes the console.log method after the all current tasks are executed as it is placed at the back of the queue

4 Comments

By the queue are you referring to the call stack if not what represent the queue?
geekabyte.io/2014/01/… I think this link provides a good explanation about how js is executed in a single-threaded manner and about the event queue, specifically with respect to setTimeout.
So when the setTimeout method completes it is placed at the call stack and executed is that correct?
yeah only after all the existing tasks being executed have been completed.
1

It is queued on libuv. V8 engine has own execution stack. When times up and main thread is available, v8 engine's next execution stack will be

    () => {
     console.log('Hello');
    }

Comments

0

See the specification for the "timer initialization steps":

  1. If method context is a Window object, wait until the Document associated with method context has been fully active for a further timeout milliseconds (not necessarily consecutively).

After this finishes, the function gets executed; the task for the function only gets queued after this point. Before this point, it only exists inside the browser's internal (implementation-dependent) timer code.

4 Comments

I not sure if I understand when you say browser. I understand that setTimeout is a javascript method but there is no browser implementation.
Each different browser has code that implements setTimeout. For example, the internals of IE's code that runs when setTimeout is called from JavaScript is not the same as Chrome's, which is not the same as Firefox's, which is not the same as Safari's, etc. The browsers are not all built using the same internal code.
Node doesn't follow html specs here.
Could you explain what means this step 15?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.