【问题标题】:Get time that all query finished获取所有查询完成的时间
【发布时间】:2017-01-26 03:38:15
【问题描述】:
function runAll(){
    for (var i = 0; i < Math.random(); i++) {
        pool.connect(function(err, client, done) {
            client.query("update   box set gamer_id=null where  box_id=$1; ", [i], function(err, resultUpdate) {
                if(err)
                    return "has error"
                //when all query finished then return value
                return "finished all query";

            })
        })
    }

    return "after for";
}

这个函数总是返回“after for”,但我想在所有查询完成后返回“finished all query”,因为这些查询可能有一些错误。

所有查询完成后如何返回值?

我不能使用 pg-promise for ...

【问题讨论】:

标签: node.js node-postgres


【解决方案1】:

试试这个,

function runAll(cb){
    var total = Math.random()
    for (var i = 0; i < total; i++) {
        pool.connect(function(err, client, done) {
            client.query("update   box set gamer_id=null where  box_id=$1; ", [i], function(err, resultUpdate) {
                i==total-1 && cb(err, resultUpdate)
            })
        })
    }
}

console.log("before invoke of runAll")

console.time("dbprocess");
runAll(function allDone(err, res){
  console.log("after call of runAll completed");
  console.timeEnd("dbprocess");
  if(err)
      return "has error"
  //when all query finished then return value
  return "finished all query";

})

console.log("after invoke of runAll")

对于 JS 中的计时器,另请参阅此 SO,https://stackoverflow.com/a/18427652/4466350

我觉得你不需要关于回调等的解释,但我可能有错,请告诉我们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 2010-11-06
    • 1970-01-01
    相关资源
    最近更新 更多