Dealing with the node.js callback pyramid

Posted by thecoop on Programmers See other posts from Programmers or by thecoop
Published on 2013-10-23T09:53:47Z Indexed on 2013/10/24 10:10 UTC
Read the original article Hit count: 266

Filed under:
|

I've just started using node, and one thing I've quickly noticed is how quickly callbacks can build up to a silly level of indentation:

doStuff(arg1, arg2, function(err, result) {
    doMoreStuff(arg3, arg4, function(err, result) {
        doEvenMoreStuff(arg5, arg6, function(err, result) {
            omgHowDidIGetHere();
        });
    });
});

The official style guide says to put each callback in a separate function, but that seems overly restrictive on the use of closures, and making a single object declared in the top level available several layers down, as the object has to be passed through all the intermediate callbacks.

Is it ok to use function scope to help here? Put all the callback functions that need access to a global-ish object inside a function that declares that object, so it goes into a closure?

function topLevelFunction(globalishObject, callback) {

    function doMoreStuffImpl(err, result) {
        doMoreStuff(arg5, arg6, function(err, result) {
            callback(null, globalishObject);
        });
    }

    doStuff(arg1, arg2, doMoreStuffImpl);
}

and so on for several more layers...

Or are there frameworks etc to help reduce the levels of indentation without declaring a named function for every single callback? How do you deal with the callback pyramid?

© Programmers or respective owner

Related posts about node.js

Related posts about code-style