我正在努力将此循环更改为异步 nodejs


  • 循环 -
    1. 从mysql获取一些数据
    2. 将数据插入特定数组(在i的索引上(

,但是如您所知,'我的增长比我预期的要早得多。我知道这是Nodejs同步/异步的问题。我想将其更改为同步函数,但是这太令人困惑了,因为这是我的第一次...

...
dbconn.pool.getConnection(function (err, conn) {
...
    for (var i = 0; i < search_result.length; i++) {
        console.log("1, i here : "+i)
        conn.query(sql_getId_and_count, [search_result[i].address], function (err, result) {
              console.log("2.i here:" + i)
              if (err) console.error(err)
              else {
                   console.log("here : " + result[0].counter)
              }
        })
    }
})

您可以在循环时使用。请注意,node.js并未使while循环中断。

您也可以使用process.nexttick((,并在其中编写实现。例如:process.nexttick(function(({}(

最新更新