2

I am writing my very first command line tool with node.js. I'm familiar with the concept of async io and understand the benefits of using it. I am wondering however, since this tool will do the following:

  1. Perform some FS operations (e.g. create folder, write to some files)
  2. Make a network request (e.g. download some stuff into this folder)
  3. Produce a result (meaning a bunch of files)
  4. Exit

Should I really be using all the async versions for these methods? for instance mkdirp() is also available as mkdirpSync() (from the fs-extra module), so the real question is:

Why do I even have to bother using async functions in a program that is due to terminate upon completion? and has no real impact in case the application thread is blocked?

Thanks.

2 Answers 2

3

Short answer: in described scenario you don't have to bother.

Long answer: it depends whats the real purpose of this application. If thats just quick & dirty prototype to test something or show your boss that you can make node apps - use async or sync - noone will care.

But if you are planning give this app back to community you should definitely think about async approach. If someone will stumble upon your code & will want to reuse it e.g. in gulp/grunt task then what? Your code will be useless until someone will rewrite it as async.

Additionally as you mentioned this is your first app in node so you should from start get in to proper habit e.g. use async functions while dealing with file system or network.

If you are annoyed by all this callbacks in async approach try to incorporate to your work-flow something like promises (e.g. amazing Bluebird or q) - this will help you achieve very clean & easy to maintain code which is completely asynchronous + can be written in procedural style!

Hope this will help!

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

Comments

1

Im not much of am expert in nodejs, but i do develop parts of my application in node.
You are welcome to correct me if im wrong

Why you have to bother using async functions is when you run a sync script the entire process halts. Nodejs is basically an event loop so when you run a sync function that loop stops until the sync function is completed which destroy its purpose.

If you application is meant for a single user then in my opinion it wont be much of an issue, but if it is meant for multiple users then you should use async functions.

In your case if you use sync function for FS operations then during that time you wont be able to make any other requests to node server. Even though it wont take much time, its always advisable to use async functions.

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.