Javascript 节点js异步模块系列依赖项

Javascript 节点js异步模块系列依赖项,javascript,node.js,asynchronous,Javascript,Node.js,Asynchronous,我想知道如何使用Node.js中的异步库处理依赖关系,请看以下示例: db.open(function(error, client) { client.collection(('my-collection', function(error, collection) { collection.insert({ "email": "test@gmail.com" }, function(error, docs) { // Do stuff. }); }); });

我想知道如何使用Node.js中的异步库处理依赖关系,请看以下示例:

db.open(function(error, client) {
  client.collection(('my-collection', function(error, collection) {
    collection.insert({ "email": "test@gmail.com" }, function(error, docs) {
      // Do stuff.
    });
  });
});
使用:


您使用的是async Parallel,它将所有函数一起运行,并且它们可以按任意顺序完成

您可以使用异步瀑布函数将变量从一个回调传递到下一个函数,例如

    async.waterfall([
    function(callback){
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback){
        callback(null, 'three');
    },
    function(arg1, callback){
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
   // result now equals 'done'    
});
或者您可以使用auto函数,它允许您指定哪些其他函数必须首先完成

async.auto({
    get_data: function(callback){
        // async code to get some data
    },
    make_folder: function(callback){
        // async code to create a directory to store a file in
        // this is run at the same time as getting the data
    },
    write_file: ['get_data', 'make_folder', function(callback){
        // once there is some data and the directory exists,
        // write the data to a file in the directory
        callback(null, filename);
    }],
    email_link: ['write_file', function(callback, results){
        // once the file is written let's email a link to it...
        // results.write_file contains the filename returned by write_file.
    }]
});
因此,在你的情况下,你能做的是:

async.auto({
    dbReady: function(callback) {
       db.open(function(error, client) {
         callback(error, client);
       });
    },
    connected: ['dbReady', function(callback, results){
       // How do I access the "client" variable at this point?
       console.log(results.dbReady);
    }
},
function(results){
   // Do stuff.
});

您使用的是async Parallel,它可以同时运行所有函数,并且可以按任意顺序完成

您可以使用异步瀑布函数将变量从一个回调传递到下一个函数,例如

    async.waterfall([
    function(callback){
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback){
        callback(null, 'three');
    },
    function(arg1, callback){
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
   // result now equals 'done'    
});
或者您可以使用auto函数,它允许您指定哪些其他函数必须首先完成

async.auto({
    get_data: function(callback){
        // async code to get some data
    },
    make_folder: function(callback){
        // async code to create a directory to store a file in
        // this is run at the same time as getting the data
    },
    write_file: ['get_data', 'make_folder', function(callback){
        // once there is some data and the directory exists,
        // write the data to a file in the directory
        callback(null, filename);
    }],
    email_link: ['write_file', function(callback, results){
        // once the file is written let's email a link to it...
        // results.write_file contains the filename returned by write_file.
    }]
});
因此,在你的情况下,你能做的是:

async.auto({
    dbReady: function(callback) {
       db.open(function(error, client) {
         callback(error, client);
       });
    },
    connected: ['dbReady', function(callback, results){
       // How do I access the "client" variable at this point?
       console.log(results.dbReady);
    }
},
function(results){
   // Do stuff.
});

Wow,从未见过如此复杂的函数表达式:
function(callback){db.open(function(error,client){callback(error,client);}}
等于
function(callback){db.open(callback);}
等于
db.open
(可能
db.open.bind(db)
)Wow,从未见过如此复杂的函数表达式:
function(callback){db.open(function(error,client){callback(error,client);}
等于
function(callback){db.open(callback);}
等于
db.open.bind(db)