Sunday, December 2, 2018

node.js - I'm struggling with changing this loop as asynchronous nodejs


  • in a loop -


    1. get some data from mysql


    2. insert the data into specific array ( at the index of i)




but as you know, 'i' increases much earlier than I expected.
I know it's a problem with a sync/async proplem of nodejs.
I wanna change it into synchronous function, but it's too confusing maybe because it's my first time...



...
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)
}
})
}
})

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...