任何方式使javascript中的嵌套回调代码更具可读性?

时间:2012-06-29 02:43:28

标签: javascript node.js syntax callback

考虑这段婴儿简单的代码

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 );
               } );
           }
        );
      }
    );
  }
);

如果以程序方式书写

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 ..." ) );
在我看来,后者更具可读性和可维护性。

是否有某种折衷方法可以使标准缩进回调样式更具可读性?

3 个答案:

答案 0 :(得分:2)

您想使用async库的waterfall机制:

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);
});

在ES5中添加到bind的{​​{1}}方法非常与异步库结合使用,因为列表中的最后一个参数通常是回调,并且在进入链条之前,通常会知道较早的值。

答案 1 :(得分:0)

程序变量并不是真正等效的 - 我假设它是同步的,而原始的可能不是。

如果您不需要关闭变量,则可以使用命名函数。类似的东西:

function f2(){}
function f1() {
    client.query("select count(1) as ct from pg_prepared_statements", f2);
}
pg.connect( {
    user: 'hhope',
    password: '...',
    port: 5432,
    host: '/var/run/postgresql',
    database: 'phiddler' },
    f1
);

答案 2 :(得分:0)

“可读”是一个意见问题,但你可以这样做:

var iter = function(cb) {
   var v = Array.prototype.slice.call(arguments);
   var f = function() {
        var g = v.shift();
        g && g(f);
   };

   f();
};

iter(function(cb) {
        console.log('in f1');
        cb();
     },
     function(cb) {
        console.log('in f2');
        cb();
     });
相关问题