3

One of NodeJS's greatest features is that it is asynchronous out of the box from what I am reading, however as a beginner to NodeJS it's kind of confusing why modules like async exist if this is already being handled natively?

https://www.npmjs.com/package/async

I assume there is a good reason why but it's not obvious to me. Is it to handle callback hell or Pyramid of Doom.

Aruna
  • 11,959
  • 3
  • 28
  • 42
jmcgrath207
  • 1,317
  • 2
  • 19
  • 31
  • 1
    I have no idea what you mean by saying "nodejs is asynchronous out of the box". I don't know what it means to say any language or implementation is "asynchronous". Do you mean that certain features of JS, such as functions as first-class objects which can be used as callbacks, or non-blocking I/O, lend themselves to asynchronous programming? Where did you read this? –  Nov 07 '16 at 05:05
  • @torazaburo https://www.codementor.io/codeforgeek/manage-async-nodejs-callback-example-code-du107q1pn – jmcgrath207 Jun 14 '17 at 05:03
  • This is roughly like saying, "ham is food out of the box, so why do I need to make a sandwich?". –  Jun 14 '17 at 05:18
  • @torazaburo an egg is still a boneless chicken – jmcgrath207 Jun 14 '17 at 05:23

2 Answers2

8

Read the description:

Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript.

It doesn't "provide" asynchronous functions, it provides functions for working with asynchronous javascript.

Note: javascript is not all asynchronous, just the asynchronous parts are asynchronous.

To put it another way

async doesn't make nodejs asynchronous, it makes using asynchronous code simpler through its sugar coated goodness

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • I understand that javascript is not asynchronous but the nodejs runtime is, so why would I use the async module in nodejs? – jmcgrath207 Nov 07 '16 at 05:05
  • `but the nodejs runtime is` is what? asynchronous? where did you read this? Also, you clearly don't understand that the `async` library provides functions **for working with asynchronous javascript** - it supplies simple "sugar" functions for common asynchronous "patterns" – Jaromanda X Nov 07 '16 at 05:06
  • I found it here http://stackoverflow.com/questions/10570246/what-is-non-blocking-or-asynchronous-i-o-in-node-js?noredirect=1&lq=1 and once again this question is only directed at nodejs and not vanilla **javascript** – jmcgrath207 Nov 07 '16 at 05:13
  • 1
    Applogies, I do see nodejs described as **an asynchronous event driven JavaScript runtime** in the nodejs about page - however, async doesn't MAKE nodejs asynchronous, it helps write simpler asynchronous code – Jaromanda X Nov 07 '16 at 05:16
  • Thank you, so for example I would use something like async module with traditional java script. My assumption is that the NPM is a repo only for nodejs. – jmcgrath207 Nov 07 '16 at 05:18
  • 1
    No, you would use async to make writing asynchronous code "easier" (well, it's easier for some) - think of it this way - Many client side developers use jQuery - jQuery doesn't ADD anything to javascript that it can't already do, it **is** javascript for a start, and all it does is make common tasks easier ... async does that with common asynchronous tasks – Jaromanda X Nov 07 '16 at 05:20
2

When you use asynchronous programming in NodeJS, you may end up with Callback Hell or Pyramid of Doom when you have more number of asynchronous functions to be called one after another one as below.

Callback - Once your first function is executed asynchronously, your main thread should be notified about it. For which you are passing a function as callback which will be fired once the asynchronous operation completes.

When you have more number of asynchronous functions in chain or inside a big loop, you may have to pass the same number of callbacks to find out the completion of each operation and the last one to perform other stuffs such as returning the response etc.

When you code them with more number of callbacks, it becomes very hard to manage/maintain and lacks better readability like the one below.

getData(function(a){  
    getMoreData(a, function(b){
        getMoreData(b, function(c){ 
            getMoreData(c, function(d){ 
                getMoreData(d, function(e){ 
                    ...
                });
            });
        });
    });
});

To get rid of these disadvantages and for better readability and maintenance, we may go with other modules such as async, bluebird etc. You can choose whatever you like which seems to be better for you in terms of understanding and satisfying all the requirements without making things too complex.

Anyways, this is purely up-to-you to go with the callback hell or other modules.

To get into the deeper insights,

https://strongloop.com/strongblog/node-js-callback-hell-promises-generators/

Aruna
  • 11,959
  • 3
  • 28
  • 42