Sunday 2 December 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...