Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

any way to make nested callback code in javascript more readable?

any way to make nested callback code in javascript more readable?

Problem

consider this baby simple piece of code

pg.connect( {
    user: 'hhope',
    password: '...',
    port: 5432,
    host: '/var/run/postgresql',
    database: 'phiddler' },
  function( err, client ) {
    client.query(
      "select count(1) as ct from pg_prepared_statements",
      function( err, results ) {
        console.log( 'prepared statement count: '+results.rows[0].ct );
        client.query( {
                    name: 'test2' ,
                    text: "insert into t1( c2 ) values( 'q1')" },
          function( err, result ) {
            console.log( 'inserted t1' );
            client.query(
              "select count(1) as ct from pg_prepared_statements",
               function( err, results ) {
                  console.log( 'prepared statement count: '+results.rows[0].ct );
               } );
           }
        );
      }
    );
  }
);

if written procedurally would be

pg = new PG(...);
client = new pg.client();
console.log( client.col( "select ..." ) );
client.prepare( 'test2', "insert into t1( c2 ) values( 'q1')" );
console.log( client.col( "select ..." ) );

in my mind the latter is far more readable and maintainable.

is there some sort of compromise approach that would make the standard indented callback style more readable?

Problem courtesy of: cc young

Solution

You want to use the async library's waterfall mechanism:

var async = require('async');

async.waterfall([
    pg.connect.bind(this, {
        user: 'user', password: 'pass', port: 5432, host: '/somewhere/', database: 'db'
    }),
    function(client, callback) {
        client.query('select * from foo', callback)
    }
], function(err, results) {
    console.log(results);
});

The bind method added to Functions in ES5 is very useful in combination with the async library, since often the last argument in the list is a callback, and the earlier values are often known before entering the chain.

Solution courtesy of: David Ellis

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

any way to make nested callback code in javascript more readable?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×