Node.js is not suitable for generic web projects (II)
Part I • Part II • Part III • Part IV • Part 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:
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:
Written by Ionut-Cristian Florescu
Related protips
8 Responses
Sorry for having to break it up into 5 parts, but apparently there's a limit on the text size somewhere...
Promises are good to avoid callback hell.
Hi, nice article. I was wondering what theme is used in Sublime Text on the screenshots? Thanks.
@sheerun - agreed. But for those preferring CoffeeScript, ICS is also a good alternative :-)
@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.
Great! Thank you!
@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?
If you write blocking code it doesn't matter if you're using Nodejs or not.