通过Node.js中的预定义函数传递参数

通过Node.js中的预定义函数传递参数,node.js,Node.js,我正在努力在Node.js中传递数据概念。 让我们以SQL冗长为例。下面是示例中的代码: //acquire a connection pool.acquire(function poolAcquire(err, connection) { if (err) { console.error(err); return; } //use the connection as normal var request = new Request('select 1;select 2

我正在努力在Node.js中传递数据概念。 让我们以SQL冗长为例。下面是示例中的代码:

//acquire a connection
pool.acquire(function poolAcquire(err, connection) {
    if (err) { console.error(err); return; }

    //use the connection as normal
    var request = new Request('select 1;select 2;select 3', function requestCallback (err, rowCount) {
        if (err) { console.error(err); return;}

        console.log('rowCount: ' + rowCount);

        //release the connection back to the pool when finished
        connection.release();
    });

    request.on('row', function onRequestRow(columns) {
        console.log('value: ' + columns[0].value);
    });

    connection.execSql(request);
});
pool.acguire将函数作为参数,此函数具有特定的签名(err,connection)

我的问题是-如何在这个函数中传递SQL语句? 我无法更改签名,因为未调用不同的函数签名

此外,我不能使用全局范围,因为变量可能在外部更改

换句话说,我需要找到绕过wrappers调用并仍然传递一些数据的方法

差不多

var mySQLs = ['select 1;select 2;select 3','select 4;select 5;'];
async.forEach(mySQLs,WorkWithOneItem, AllIsDone);

function WorkWithOneItem(item, callback){
    pool.acquire(?????(item));
    callback(); // tell async that the iterator has completed
} 

function AllIsDone (err) {
    console.log('All done');
}

通过将其包装到另一个函数中:

function aquire(sql, callback) {
    pool.acquire(function poolAcquire(err, connection) {
        if (err) { console.error(err); return callback(); }

        //use the connection as normal
        var request = new Request(sql, function requestCallback (err, rowCount) {
            if (err) { console.error(err); return;}

            console.log('rowCount: ' + rowCount);

            //release the connection back to the pool when finished
            connection.release();
            callback();
        });

        request.on('row', function onRequestRow(columns) {
            console.log('value: ' + columns[0].value);
        });

        connection.execSql(request);
    });
}

function WorkWithOneItem(item, callback){
    acquire(item, () => {
        callback(); // tell async that the iterator has completed
    });
} 

通过将其包装到另一个函数中:

function aquire(sql, callback) {
    pool.acquire(function poolAcquire(err, connection) {
        if (err) { console.error(err); return callback(); }

        //use the connection as normal
        var request = new Request(sql, function requestCallback (err, rowCount) {
            if (err) { console.error(err); return;}

            console.log('rowCount: ' + rowCount);

            //release the connection back to the pool when finished
            connection.release();
            callback();
        });

        request.on('row', function onRequestRow(columns) {
            console.log('value: ' + columns[0].value);
        });

        connection.execSql(request);
    });
}

function WorkWithOneItem(item, callback){
    acquire(item, () => {
        callback(); // tell async that the iterator has completed
    });
} 

你也需要结果吗

var mySQLs = ['select 1;select 2;select 3','select 4;select 5;'];
async.forEach(mySQLs, WorkWithOneItem, AllIsDone);

function WorkWithOneItem(sql, callback){
  pool.acquire(function poolAcquire(err, connection) {
      if (err) return callback(err);

      //use the connection as normal
      var request = new Request(sql, function requestCallback (err, rowCount) {
          if (err) return callback(err);

          console.log('rowCount: ' + rowCount);

          //release the connection back to the pool when finished
          connection.release();
      });

      var rows = [];
      var count = 0;
      request.on('row', function onRequestRow(columns) {
          console.log('value: ' + columns[0].value);
          rows.push(columns[0].value); // or whatever you actually want out of these.
      });

      request.on('done', function onRequestDone() {
        callback(null, rows);
      });

      connection.execSql(request);
  });
  callback(); // tell async that the iterator has completed
} 

function AllIsDone (err) {
  console.log('All done');
  // you probably want async.map, so you can get the result back
  // as the second argument for a function like this
}

你也需要结果吗

var mySQLs = ['select 1;select 2;select 3','select 4;select 5;'];
async.forEach(mySQLs, WorkWithOneItem, AllIsDone);

function WorkWithOneItem(sql, callback){
  pool.acquire(function poolAcquire(err, connection) {
      if (err) return callback(err);

      //use the connection as normal
      var request = new Request(sql, function requestCallback (err, rowCount) {
          if (err) return callback(err);

          console.log('rowCount: ' + rowCount);

          //release the connection back to the pool when finished
          connection.release();
      });

      var rows = [];
      var count = 0;
      request.on('row', function onRequestRow(columns) {
          console.log('value: ' + columns[0].value);
          rows.push(columns[0].value); // or whatever you actually want out of these.
      });

      request.on('done', function onRequestDone() {
        callback(null, rows);
      });

      connection.execSql(request);
  });
  callback(); // tell async that the iterator has completed
} 

function AllIsDone (err) {
  console.log('All done');
  // you probably want async.map, so you can get the result back
  // as the second argument for a function like this
}