如果一个action中有多次查询,conn.end是写在哪个回调里面?
比如
var mysql = require('mysql');
var conn = mysql.createConnection(param);
conn.query("查询1",function(err,data){ });
conn.query("查询2",function(err,data){ });
conn.query("查询3",function(err,data){ });
4 回复
用 async 管理多个查询,都执行完了再 call conn.end
var EventProxy = require('eventproxy');
var ep = new EventProxy();
ep.all('sql1', 'sql2', 'sql3', function (data1, data2, data3) {
// 成功回调
});
// 添加error handler
ep.fail(callback);
var mysql = require('mysql');
var conn = mysql.createConnection(param);
conn.query("查询1",ep.done('sql1'));
conn.query("查询2",ep.done('sql2'));
conn.query("查询3",ep.done('sql3'));
我也来一段async
var async = require('async');
var mysql = require('mysql');
var conn = mysql.createConnection(param);
var sqls = ['查询1',"查询2",“查询3”];
async.mapSeries(sqls,function(sql,callback){
conn.query(sql,callback);
},function(err,res){
//所有任务完成后,结果都在这里,res是结果
})
var async = require(‘async’);
var mysql = require(‘mysql’); var conn = mysql.createConnection(param);
var sqls = [‘查询1’,“查询2”,“查询3”]; async.mapSeries(sqls,function(sql,callback){ conn.query(sql,callback); },function(err,res){ //所有任务完成后,结果都在这里,res是结果 })