End callback hell in your Node server code

ayasin
2 min readJan 16, 2015

--

Dealing with multiple layers of callbacks sucks. There are a few libraries (like the excellent “async”) that try to help with the situation but ultimately it’s lipstick on a pig. Awesome developers like TJ Holowaychuk (the original author of ExpressJS), in his Farewell to Node wrote of the state of Node callbacks (on of the key reasons he listed for leaving Node):

you may get duplicate callbacks

you may not get a callback at all (lost in limbo)

you may get out-of-band errors

emitters may get multiple “error” events

missing “error” events sends everything to hell

often unsure what requires “error” handlers

“error” handlers are very verbose

callbacks suck

Personally I’m done with dealing with these issues which is why I started a project at the start December 2014 to really solve them.

FRHTTP is an alternative to ExpressJS/Connect and other callback based HTTP servers that solves all these problems. It also interoperates with ExpressJS so if you want to try it out for a single route and see how you like it, you can do that.

How does FRHTTP address callback hell? By not using callbacks. FRHTTP is a functional reactive system. I’ll expound much more on this in future blog posts…if you’re interested in a sneak peek, take a look at the README on GitHub.

It’s currently a work in progress (it has enough for you to actually play with it, but there’s more to do) and as part of working on it I’m planning to write a series of blog posts to help bootstrap new developers in how to use it.

As I mentioned earlier, for now, there’s a README.md in the Github repo that tells you alot about the current state of the project, and there’s a samples directory with some basic samples.

If you want to give it a go, you can install it via npm and run the sample server you can go to the frhttp directory as below:

This will start a server on localhost port 8001. At this point you can start hitting several /sample/ api endpoints including:

  • /samples/isSquareRoot/4/2 (you can replace /4/2 with whatever 2 numbers you want)
  • /samples/querySimulator/10
  • /samples/hello
  • /samples/uncaught
  • …others

If you’re interested in a callback free, functional reactive way to write Node, watch this space…this is just the beginning.

--

--