mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.27k stars 2.52k forks source link

mysql.createPool transaction with loop #2540

Closed how1231 closed 2 years ago

how1231 commented 2 years ago

I'm trying to change mysql connection from mysql.createConnection(config) to mysql.createPool(config). So far most of the APIs required nothing much to change except for transaction.

Problem: There is a for loop going to execute query inside a pool transaction callback hell. But the loop was not waiting for the callback to finish.

Question: Does anyone has any idea on how to solve this kind of asynchronous problem?

ps: Sorry for the long question...I'm already stuck at here for few days, any help would be appreciated, thanks in advance!

dougwilson commented 2 years ago

Hello, and sorry you are having trouble. Yes, in Node.js using async callbacks in a for loop doesn't work as you would like. Thankfully there is a great library that provides this functionality: https://www.npmjs.com/package/async