LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 September 26 2015

Alloushi
Member

Any node.js programmers

I've been mainly developing PHP scripts and now that I got into node.js it's quite understanding until you write a 100 line code and debug it when you get into a hole full of sadness called the callback hell.

I'm in this callback hell and I'm currently trying to understand how to get out of it, other than that, I need to implement something to control flow of my script. this stupid server-side script is not easy to handle.

However even if it's annoying to deal with node.js it still offer some advantages.

Anyone would help me with callback hell would be appreciated

Offline

#2 September 26 2015

rolf
Member

Re: Any node.js programmers

Hello,
I'm a PHP dev by experience but working now with people who use node specifically for running grunt tasks, in other words for building client-side HTML, CSS and JavaScript.
I think node is good for such things. I don't believe it to be a good replacement for PHP anymore, not that I ever really tried to replace PHP with it.
Anyway if you're in "callback madness", then consider using events instead of callbacks. The thing with events is that they need to usually be attached to the DOM - but you could create your own "event bus"  object (it's really very simple) and be using that for sending and catching events between parts of your code.
You could also look into promises which is a new feature of Javascript which tries to solve some problems with events.
And of course be careful of how you organize your code.

Last edited by rolf (September 26 2015)

Offline

#3 September 26 2015

Alloushi
Member

Re: Any node.js programmers

Thank you for your reply, I've read a tutorial about event emitters, I'm currently looking forward into implementing it.

Offline

#4 October 12 2015

rolf
Member

Re: Any node.js programmers

Also one thing I've noticed going back and forth from node to PHP is that PHP seems to be more restrictive. With the new stuff (anonymous functions, closures, etc.), it should be better, but it was my impression anyway. PHP has a rich library but seems to be more imperative and somehow restrictive in how to write programs.
JavaScript allows for different code writing styles and functional approaches.

Offline

Board footer