Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript event loop: Where do web api's get executed?

In reading about the JS event loop, I was curious where web api's get executed. For example, in this image, the pink box doesn't have a name (just says "implementation specific"), leaving me to wonder where these web api's get executed. The call stack, event loop, message queue all have names. I've also seen Philip Roberts talk about the event loop and he just refers to it as a "Web API".

So when a web api is reached in the call stack, it goes off and gets executed while the rest of the call stack gets run. But where does that web api call go to be executed? For example, if there's a set time out of 5 seconds, where is it sitting for those 5 seconds?

like image 353
TheRealFakeNews Avatar asked Aug 01 '16 18:08

TheRealFakeNews


1 Answers

Even though this was the question from 2 years ago, as there is no answer yet, I will try to answer this question based on my understanding. This is currently how I understand javascript, it may be not accurate everywhere since I just started to code half a year. If there is a mistake, please point out.

The simple answer to this question is: it is still running on your computer, but it is not in the same thread as your execution stack.

First, I want to talk about v8 engine. V8 is one of the javascript engines and javascript engine is mostly provided by browser vendors. The very first javascript engine was created by the same person who invented javascript. Also, In the video, he mentioned setTimeout is not implemented in v8, V8 is just what chrome is using to understand javascript. Therefore, I will say Javascript is just a bunch of letters, but only with javascript engines, such as V8: it understands how javascript thinks. [Side Note: node.js is implemented based on V8 engine, and I will explain your question based on node.js.]

Second, the word api should be clarified. I don't like to talk about definitions, so I will state my own understanding: api is just an interface to make things easier for programmers. It hides difficult works from the user[developers who are using the api]. When we talk about web-api, it can be any client-side api, such as youtube api, of course, for the server side, there is restful api too. If you go to check out the node.js github, you will see setTimeout is implemented in lib/times.js with language of javascript. In conclusion, the web-api he was talking in the video, is just an interface created with javascript.

Third, the statement javascript is single-threaded is not 100% true. Javascript runs multiple threads in the background. Of course, if you look at the diagram of event loop, and think about how the queues and event loop works, you may notice that it is impossible with one single thread. Only with multiple threads, javascript is able to run things asynchronously.

Conclusion: V8 did not implement setTimeout, DOM, so I guess engine natively does not understand setTimeout. While, setTimeout, DOM etc are implemented beyond javascript. Then, when you were calling setTimeout in javascript, it is not compiled directly by engine, but calls another function which is written in javascript. And the body of that function can be compiled by engine in another thread.

Q1:

where does that web api call go to be executed?

A1:

Web-api is just being called like a Javascript function, or youtube api, etc

Q2:

where is it sitting for 5 seconds?

A2:

Your computer, but a different thread.

like image 162
Eric Quan Avatar answered Nov 07 '22 08:11

Eric Quan