5

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);  
5
  • Can you post an example of async code ? Commented Sep 18, 2018 at 12:22
  • You should read some articles about Node's event loop (like this one : blog.risingstack.com/…) Edit : This answer gives a good explanation too : stackoverflow.com/questions/25568613/node-js-event-loop Commented Sep 18, 2018 at 12:23
  • 1
    Possible duplicate of Node.js Event loop Commented Sep 18, 2018 at 12:26
  • 1
    The Event loop is fairly complex, but understanding (parts of) it will help you understand the answer to your question. Here's a great conference talk by Philip Roberts at JSConf 2014 explaining the Event loop: youtube.com/watch?v=8aGhZQkoFbQ Commented Sep 18, 2018 at 13:14
  • The sample code provided, just shows a lack of understanding of the technology used, which is a problem that many enthusiast will run into at the beginning. But given guidance and learning, one should understand the concepts relatively fast, some goes for those nice global declarations or not checking if an error had occured (so from where I read this question, the code doesn't really give any benefit to the question at hand) Commented Sep 19, 2018 at 22:46

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.