On Mon, Mar 19, 2018 at 10:11 AM, Kelly Cookson <KCookson@xxxxxxxxxxxx>
wrote:
Henrik,
Being a newbie, I am curious. Why would I want to use Node if I’m not
leveraging the event loop with asynchronous programming?
I'm not aware of any way to avoid the event loop in Node.js. It appears
that you can block the cycle from running by coding something like a tight
for-loop (i.e. for i = 1 to 100000000). That would be bad form, of course.
Something like that would block any other scripts from running in that
server. If you ran that in the master process that had forked a set of
child processes, then it would block any inter-process communication that
any applications may rely on.
Perhaps Henrik hadn't put much thought into his message. It wasn't clear
(to me at least) whether his reference to asynchronous programming was in
regard to the new "async" and "await" keywords which make functions appear
to wait for things like DB I/O to occur. They don't actually wait. It's
just a clever way to implement callbacks in Node.js without passing a
reference to callback functions. The actual callback is to the same
function code following the "await" keyword. The function appears to resume
where it left off during an I/O operation, or the like. Several cycles of
the event loop could have actually occurred in between, while the I/O had
actually been passed off to another thread in the thread pool.
I understand in theory that I could have an instance of the event loop
running that processes a batch script instead of handing it off to worker
threads. But that why would I want to do that? I’m genuinely curious since
I’m still learning and this may be a failure of my imagination.
I'm curious too. Particularly if the "batch script" were performing any DB
I/O. Why would you do that in Node.js?
As an Amazon Associate we earn from qualifying purchases.