Skip to content

feature: Synchronous prefix to solve pyramids-of-doom (while being backwards compatible) #104

@gladys123

Description

@gladys123

Pyramids of doom are a common issue with the callback orientated architecture. While they can be countered in many ways, I believe there's a need for a simple, versatile, and backwards compatible feature that fixes it.

&: the call synchronously prefix.

console.log( &request('https://www.google.com').body ) is equivalent to

request('https://www.google.com', function(err, status, body){
    console.log(body);
});

Effect

Execution of the rest of the block is paused until the async function returns, making it effectively synchronous. The callback parameters are returned, as an object.

Example

Before:

socket.on("event", function(data){
    db.query("SELECT foo, bar FROM moo WHERE 1", function(err, rows){
        if (err)
            // handle
        if (rows.length){
            request('http://external.api/' + rows[0].foo, function(err, status, body){
                request('http://external.api/' + rows[0].bar, function(err2, status2, body2){
                    asyncFunction(foo, bar, function(response){
                        socket.emit("response", response);
                    });
                });
            });
        }
    });
});

After:

socket.on("event", function(data){
    var res = &db.query("SELECT foo, bar FROM moo WHERE 1");
    if (res.err)
        // handle
    if (res.rows.length){
        var fooReq = &request("http://external.api/" + res.rows[0].foo).body;
        var barReq = &request("http://external.api/" + res.rows[0].bar).body;
        socket.emit("response",  &asyncFunction(fooReq, barReq));
    }
});

Compatibility

This would allow calling any async function synchronously. It can be implemented with some simple pre-processing, without any changes to V8 and it would be 100% backwards compatible with existing code.

  • Callbacks with one return parameters will return it directly. Bonus: Resolves boilerplate if a function returns multiple parameters as an object.
  • Callbacks with multiple return parameters will return it as an object. Bonus: Parameters are order-indepedent.

Do we really need it?

I think this is the simplest solution. Pyramids of doom are faced by people new to node universally, and this resolves it without the need of any external modules.

With io.js, let's advance the platform and embrace useful change that are backwards compatible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions