Javascript callback function in separate thread

Javascript is single threaded. So the callback function and the function containing it are executed in the same thread as the main loop / event loop?

database.query("SELECT * FROM hugetable", function(rows) { // anonymous callback function var result = rows; console.log(result.length); }); console.log("I am going without waiting..."); 

If the query () method and its callback function are executed in the same thread as the event loop, blocking will occur. If not why is Javascript called single-threaded?

Can anyone help verify that javascript (browser / node.js) uses multiple threads behind the scenes to achieve non-blocking?

Thanks,

Friends, I saw your comments and answers. Sorry, I'm very new to javascript. I am confused that a single-threaded asyn call will not block. If 100 users request data from a huge table that can go through every minute every time, and the event loop sends these tasks to the queue and executes them in turn, as the query () method does not block the event loop, since they are all included in one single flow?

Brad answered this part.

+6
source share
4 answers

Node.js native libraries (not parts of JavaScript) use separate threads all the time, but all the data for your code is shuffled back into a single JavaScript execution thread.

It is impossible to say whether other topics are running in the background for this request, since you did not specify which database library you are using. However, this is not as important as you think.

Suppose you have to create a thread to handle a database connection. You run a query, and this thread takes care of receiving the query on the database server. So what? This thread sits around absolutely nothing until the data returns. You have effectively wasted resources maintaining a stream around that doesn't do much. Node.js does not work this way. You have one thread to execute JavaScript.

If you send or receive data (basically this is what your DB port will do), then you automatically load the background thread pool. It is also possible that any DB connector you use has its own extension, which can do anything with streams.

See my post here for a more complete explanation: fooobar.com/questions/693417 / ...

+3
source

Javascript is single threaded. So the callback function and the function containing it are executed in the same thread as the main loop / event loop?

Yes, you answered your question.

You're right, JavaScript is single-threaded . There there is no other thread to call back.

However, external libraries written in C or C ++ can create all the themes they want.

+1
source

What you do in your code defers the request evaluation (making a callback) until the request is completed and the rows are received.

In modern browsers, you can create two streams using webmasters if necessary.

Node.js is a separate story - you can create child processes that will be executed on a separate thread if desired. See this post for more details.

+1
source

Good question, javaScript is single-threaded but asynchronous, which means that for each asynchronous call, it will pause a bit in the main thread to make a callback, at least for browsers, and even there you will have web workers that will spawn a new one flow for each employee.

0
source

Source: https://habr.com/ru/post/959361/


All Articles