4

I'm not saying async of ES7, but async functions in general like callback and promisses.

So, for all I studied about NodeJS and event loop. Everything leads me to believe that NodeJS has a false sense of async.

As far as I understand this only works well when the function has to be passed through an external medium. Example, execute a read file (which will be used by the OS API), or else a request that will use the external API as well.

I found few subjects talking about it, and I would like to discuss this with you here. My questions are: Am I right with that thought? And also if there are practical ways to find out where does async work and where does it not pay? On some occasions if I am correct async will only serve to spend more memory.

6
  • 1
    So, for all I studied about NodeJS and event loop. Everything leads me to believe that NodeJS has a false sense of async - you need to study again then - one uses async functions in nodejs when the function one uses is async ... you can't decide to write synchronous code using async functions - sure, many functions have both a sync and async version, but in the majority (more than 50%) of cases, the async version is preferred Commented Jan 10, 2017 at 0:33
  • 1
    So, that's why I asked... lol Commented Jan 10, 2017 at 0:36
  • 1
    I meant to put a ;-) after the "you need to study again" - was tongue in cheek comment, not meant to be harsh at all :p Commented Jan 10, 2017 at 0:38
  • What I'm saying is that the Node only works fine with async when the async function will perform the operation at another location. As libuv. I want to know if I'm right to think so? I see a lot of people using async gone crazy for everything, and maybe as long as it creates callbacks and promisses the node will not be able to run that async and will block. Commented Jan 10, 2017 at 0:41
  • nodesource.com/blog/understanding-the-nodejs-event-loop Commented Jan 10, 2017 at 0:43

2 Answers 2

3

Node.js works asynchronously, always. If you're doing some blocking I/O (such as with eg. fs.readFileSync() or other synchronous function), the complete node.js runtime process stops processing anything else during that call. Therefore, in web request processing, you never call synchronous functions (it's only acceptable in node.js command line apps and during app startup etc.)

This is just a fundamental features of node.js; a consequence of it is that node.js/JavaScript doesn't have and doesn't need synchronized thread synchronization features like eg. Java.

Technically, the only place in a running node.js process which is multithreaded is the internal libuv library, and only to compensate for missing asynchronous I/O of the host system.

You can use nodes.js timers to create artificial events if your processing isn't triggered by I/O events. You're right to assume that in general, this makes nodes.js inconvenient or outright unsuitable for CPU-bound processing.

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

Comments

1

Exactly this only works well when the function has to be passed through an external medium. This is the tradeoff with single threaded asynchronous event loop. You should avoid any CPU bound computations in node.js because it blocks the event loop so that no callbacks can be responded to.

node only yields on OS IO calls, it does NOT yield when a callback or a promise is used. Those are two ways to handle IO based calls but are not asyncronous in themselves. (i've seen quite a few projects where people create callback based APIs for synchronous code :( )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.