1

I'm trying to understand the APIs provided to JS engine by the browser.

Since JavaScript is single threaded, it has to outsource the timer to the browser so that the code execution is not blocked.

So JavaScript has to introduce the timer functionality via a API call to the browser. Am I right or wrong?

  • the `setTimeout` functionality is implemented in the same thread as the main thread. You can test this by running a long `for` or `while` loop and see that the `setTimeout` is not executed while the loop blocks the thread. If you a curious how this works see my answers to the following questions: https://stackoverflow.com/questions/29883525/i-know-that-callback-function-runs-asynchronously-but-why/29885509#29885509, https://stackoverflow.com/questions/64002700/busy-waiting-in-nodejs/64003050#64003050 – slebetman Oct 27 '20 at 03:31
  • Must be setTimeout learning time in JavaScript class.... like the 3rd time this has beena sked in past few days. – epascarello Oct 27 '20 at 03:31
  • https://stackoverflow.com/questions/28650804/does-settimeout-or-setinterval-use-thread-to-fire https://stackoverflow.com/questions/22051209/how-does-setinterval-and-settimeout-work – epascarello Oct 27 '20 at 03:33
  • @slebetman When setTimeout is being executed, code execution is not blocked, so how can it be implemented in the same thread as the main thread? – Toffee Conmigo Oct 27 '20 at 03:40
  • @epascarello Thanks a lot sir. This is exactly I'm looking for. – Toffee Conmigo Oct 27 '20 at 03:42
  • The `setTimeout` merely adds your function to a list/array of functions to execute after some time. Then it executes the rest of your code. When there is no more javascript to execute the interpreter stops executing javascript and enters what's called the **event loop**. This is simply a forever loop around the main interpreter. It then calls the asynchronous IO API of the OS (something like `select()`) which blocks the entire program but that's OK because the OS will wake up javascript if there is any event. And the previous `setTimeout` call will cause the async IO API to tell the OS... – slebetman Oct 27 '20 at 03:47
  • ... to wake the program up after a given amount of time. So the OS wakes up javascript and this causes the async IO API function to return. Using the return value javascript tries to figure out what event happened. OH.. it's the timeout - so it looks at the list/array of functions and finds the one you passed to `setTimeout`. **NOW** and **only now** long after your entire program has reached the last line and there is no more javascript to execute, **only at this time** will javascript call that function you passed to `setTimeout`. Understanding this is critical in understanding async code – slebetman Oct 27 '20 at 03:49
  • Basically asynchronous IO (also called non-blocking) in any language (not just javascript) can do parallel **waiting**, not parallel code execution. The main difference is that this is the default operating mode in javascript while in most other languages the default is blocking and you'd need to do some fancy programming to do async I/O – slebetman Oct 27 '20 at 03:51
  • setTimeout(console.log('hello'),1000); let sum =0; for(let i = 0;i<=1000000000;i++){ sum+=i; } console.log(sum); @slebetman I ran this code and 'hello' was immediately logged in the console – Toffee Conmigo Oct 27 '20 at 04:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/223664/discussion-between-toffee-conmigo-and-slebetman). – Toffee Conmigo Oct 27 '20 at 04:16

1 Answers1

-1

The setTimeout() function is actually exposed by the browser's window object as as such they aren't necessarily defined in the ECMAScript specification because they're not JavaScript features, they are features of the browser itself.

So, I say, you are right!!!

NinjaDev
  • 402
  • 2
  • 6