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

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



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);






Can you post an example of async code ?

– Mr_Thorynque
Sep 18 '18 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

– Seblor
Sep 18 '18 at 12:23







Possible duplicate of Node.js Event loop

– Seblor
Sep 18 '18 at 12:26






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

– AndreasB
Sep 18 '18 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)

– Icepickle
Sep 19 '18 at 22:46





1 Answer
1



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.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)