Hacker News new | ask | show | jobs
by niggler 4828 days ago
" the decision, made quite early in its life, to prefer callback-based APIs to promise-based ones."

Rewind to the point when nodejs was being designed. In that world, in the context of javascript, callbacks were the only real pattern that existed. XHR? callback. Doing something in the future? callback.

If you imagine node trying to leverage the javascript ecosystem, callbacks were a no-brainer.

5 comments

I'm not sure you've ever used XHR if you call it the callback pattern.

The XHR object is effectively a request and a response bundled up into one object that has promise-like traits. You attach event handlers to it to handle various state changes and scenarios, and then once you issue the request, the event handlers get invoked 0-N times. If it really was JavaScript callback-style, XHR would look like this:

    window.xmlHttpRequest("GET", url, function (result, error) { ... } );
It doesn't. setTimeout/setInterval are definitely callback-passing, but they're not exactly glowing examples of stellar API design. They return integer IDs instead of handles or objects!

Honestly, the only way to classify node's callback-heavy design as a 'no-brainer' is if you excuse the design by saying no thought was put into it beyond simply doing what a bunch of other people were doing. If you put enough thought into how large applications will be built, and how difficult it is to build scalable, maintainable libraries, callback-passing style easily loses compared to promises.

This is simply false.

Here's a discussion about removing promises from node.js in 2010:

https://groups.google.com/d/msg/nodejs/RvNoQtoWyZA/ar_lYLhK8...

Various incarnations of the Promise monad have existed for quite a while, even in JS. The oldest one I can think of is MochiKit's Deferred, inspired by Twisted's. That one worked (and still does) seamlessly with any callback code.
(Twisted was inspired by the work I just pointed to in my answer. Not to take away from yours -- I wasn't familiar with MochiKit.)
Of course, using monads for asynchronous tasks is an old trick and E has always been ahead of its time (like many other languages ...)
Maybe so, but everything in this article goes back at least to the 90s with the E programming language (http://erights.org). Doug Crockford was involved in E. (Nowadays E's Mark Miller is on the Ecmascript committee.)
> Rewind to the point when nodejs was being designed. In that world, in the context of javascript, callbacks were the only real pattern that existed.

Sure, but there were languages back then that made programming with callbacks more pleasant due to features like coroutines or first class continuations. While these might not have been on the radar for most Javascript developers the technology certainly existed back then.