Last Updated: September 09, 2019
·
34.02K
· Ionut-Cristian Florescu

Node.js is not suitable for generic web projects (II)

Part I • Part II • Part IIIPart IVPart V


Heathens versus Infidels

Second, web / software development and programming languages are almost always religious subjects, and there are lots of people out there of the opinion that JavaScript is not a "real" programming language. Well, it may not be the ideal one, but it's real enough to be the most commonly used on the Internet today.

The "callback hell"

As most Node.js developers should know, due to it's event-based nature, it's rather difficult to do sequential stuff in Node.js. In my opinion, this is maybe the greatest weakness and in the same time the greatest strength of Node.js. Strength, because, if you know your stuff, it lays the foundation to handle an incredible amount of concurrent connections, maybe an order of magnitude larger than what you'd get with PHP, .NET or RoR. Weakness, because most people are rightfully put off by something that looks like this:

Node.js callback hell

Each background operation returns its result (or error) in a callback. The good part is that your application server is not locked on waiting for that background task to complete, but free to do other stuff meanwhile. Like serving another request, to keep your website or application snappy and your visitors happy. The bad part is that if you're having lots of background operations (like accessing any kind of data source) your code begins to look ugly and incomprehensible.

The good news is there are lots of elegant ways to mitigate this: Async.js is probably the best known, but if you're using CoffeeScript you should definitely consider going a step further to "simply and powerfully streamline your asynchronous control flow" with IcedCoffeeScript.
I haven't given it much thought when I first approached Node.js a while ago, but now, after using it in a few projects, I can't really imagine coding without await and defer. I find it to be the ideal solution for the most common usage scenario, when you have either a number of sequential operations or a combination of sequential / parallel tasks:

IcedCoffeeScript - callback hell extinguished


> NEXT PAGE

Part I • Part II • Part IIIPart IVPart V

8 Responses
Add your response

Sorry for having to break it up into 5 parts, but apparently there's a limit on the text size somewhere...

over 1 year ago ·

Promises are good to avoid callback hell.

over 1 year ago ·

Hi, nice article. I was wondering what theme is used in Sublime Text on the screenshots? Thanks.

over 1 year ago ·

@sheerun - agreed. But for those preferring CoffeeScript, ICS is also a good alternative :-)

over 1 year ago ·

@knivets - Thanks for reading!

The Sublime Text theme is "Dark Soda", with "Tomorrow Night" colour scheme.

I'm also using a custom fork of "BetterCoffeeScript" plugin to highlight "await" and "defer" ICS keywords and to see the ICS -> JS compilation result whenever needed.

over 1 year ago ·

Great! Thank you!

over 1 year ago ·

@icflorescu Hi, nice article.
I am in big trouble i am developing web services in node.js express framework and using amazon server, my problem is that when i start my server using forever(sudo node forever app.js) my cpu utilization becomes 100% and server strucks off.After that we have to reboot the server again.Any suggestions?

over 1 year ago ·

If you write blocking code it doesn't matter if you're using Nodejs or not.

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip