-1

I have a confusion regarding after the settimeout finishes what does it put in the callback queue does it put the function name/function defination(that we pass as parameter to the settimeout) or the function invoked

function cb(){
  console.log("Hi!")
}
setTimeout(cb,2000) // what will be put on the callback queue after 2 sec cb or cb()

so now if i call setTimeout(cb,2000) after that what will it put in the call back queue just cb or cb()

and if i assume it puts just cb like that and when the eventloop puts the cb in the call stack it will execute the cb function So if thats the case then if i have:

function cb(num){
      console.log(num);
      console.log("Hi!")
    }

and call setTimeout(cb,2000,5) so for that case after the timer is done and if it puts only cb in the callback queue then when the event loop puts the cb into the call stack how does it remember or pass it the num parameter(the value 5 in this case) as it logs 5 to the console.

Any help is greatly appreciated as i am not able to set a mental model for this confusion.

  • Show us us the complete code – deathangel908 Apr 01 '20 at 19:54
  • this is not something about code i was wanted to know what does the web api put on the callback queue does it put the callback function defination(the parameter or callback that we pass to the setTimeout) or does it but the invoked callback function in the callback queue – Shubodeep Sengupta Apr 01 '20 at 19:59
  • `setTimeout(cb,2000)` . The address of the function which `cb` points to at the moment of calling `setTimeout`. The memory at that address contains bytecode of `cb` function – deathangel908 Apr 01 '20 at 20:34

1 Answers1

1

When you do setTimeout(cb,2000,5). You're pushing 3 arguments to the stack of underlying implementation:

  • address of cb function
  • integer 2000
  • integer 5

Chrome (or Node or w/e implementation you use) native API gets called with those arguments. Note that setTimeout this is NOT the part of javascript v8 engine.

I didn't check the source code, but I believe what happens next is: The event loop calculates a new time and remembers it along with the memory address of callback function and other arguments. When the time comes native API wakes up and triggers the native code which calls the callback and its arguments. So:

setTimeout(function() {console.log(arguments)}, 1, 2, 3, 4)
// prints [2, 3, 4]

The fact that you might be interested in it, that reference to a function can hold arguments with it:

b = function(){console.log(arguments)}
ba = b.bind(1,2,3)
setTimeout(ba, 4,5,6)
// prints [2, 3, 5, 6]
// where 1 is this, and 4 is the timeout
deathangel908
  • 8,601
  • 8
  • 47
  • 81
  • l am not able to understand can you plz elaborate like first i want a short answer of after the setTimeout runs for 2 sec what will we put in the callbackqueue the callback function to setTimeout or the address of the callback function or the the callback function invoked – Shubodeep Sengupta Apr 01 '20 at 20:16