The novice understanding of Node is that if I re-write synchronous, or in-line code, to utilize functions / callbacks, I can ensure that my code is non-blocking. I\'m curio
The accepted answer is great, but I would like to add something purely related to nonblocking
, specifically this part of the question:
Or is it because I/O events are just fundamentally blocking operations meaning that they seize control and don't give it back until done...
Asynchronous IO is possible even without a framework providing its own pool of IO worker threads. Indeed, it can be done without any multi-threading at all as long as the underlying (operating) system provides some mechanism to do non-blocking IO.
Typically this boils down to a system call like POSIX's select (or Microsoft's version of the same), or more recent variations on the same idea like Linux's epoll.
Hypothetically speaking, if we see a function like db.query
in your example, and assuming we also know that the framework providing that function does not rely on any multi-threading, then it is usually safe to conclude that:
db.query
call.while(true)
to something like libevselect
or a similar function will be used to check if any of those pending IO requests have finished yet.An SQL DB call like your db.query
probably uses network socket IO and not file IO, but from your perspective as an application developer, socket and file descriptors are handled in nearly identical ways on many operating systems, and can both be passed to select
on POSIX-likes anyway.
This is usually how single-threaded, single-process server applications "juggle" multiple simultaneous connections.