Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Node.js asynchronous AND single-threaded at the same time?

Tags:

The question title basically says it all, but to rephrase it:
What handles the asynchronous function execution, if the main (and only) thread is occupied with running down the main code block?

So far I have only found that the async code gets executed elsewhere or outside the main thread, but what does this mean specifically?

EDIT: The proposed Node.js Event loop question's answers may also address this topic, but I was looking for a less complex, more specific answer, rather then an explanation of Node.js concept. Also, it does not show up in search for anything similar to "node asynchronous single-threaded".

EDIT, @Mr_Thorynque: Running a query to get data from database and log it to console. Nothing gets logged, because Node, being async, does not wait for query to finish and for data to populate. (this is just an example as requested, NOT a part of my question)

var = data;
mysql.query(`SELECT *some rows from database*`, function (err, rows, fields) {
        rows.forEach(function(row){
        data += *gather the requested data*
    });
});
console.log(data);  
like image 675
marko-36 Avatar asked Sep 18 '18 12:09

marko-36


People also ask

How does js implement asynchronous functionality when it is a single thread?

Javascript is a single threaded language. This means it has one call stack and one memory heap. As expected, it executes code in order and must finish executing a piece code before moving onto the next.

How does node js support concurrency even though it is a single threaded application?

The reason why node js is popular despite being single-threaded is the asynchronous nature that makes it possible to handle concurrency and perform multiple I/O operations at the same time. Node js uses an event loop to maintain concurrency and perform non-blocking I/O operations.

How does node JS achieve asynchronously without multithreading?

Node is multithreaded. The main event loop is single-threaded by nature. But the I/O is run on separate threads/processes, because the I/O APIs in Node. js is asynchronous/non-blocking by design, in order to accommodate the singlethreaded event loop.

Is node js single threaded or multithreaded?

Node. js runs JavaScript code in a single thread, which means that your code can only do one task at a time. However, Node. js itself is multithreaded and provides hidden threads through the libuv library, which handles I/O operations like reading files from a disk or network requests.


1 Answers

What it really comes down to is that the node process (which is single threaded) hands off the work to "something else". This could be the OS's I/O process, or a networked resource or whatever. By handing it off, it frees its thread to keep working on the next in-memory thing. It uses file handles to keep track of any pending work and in the event loop marries the two back together and fire the callback when the work is done.

Note that this is also why you can block processing in your own code if poorly designed. If your code runs complex tasks and doesn't hand off the work, you'll block the single thread.

This is about as simple an answer as I can make, I think that the details are well explained in the links in your comments.

like image 131
Paul Avatar answered Sep 28 '22 19:09

Paul