I wrapped a function writeTransaction()
with Meteor._wrapAsync
and called it 5 times in a for
loop which writes a MySQL transaction.
However from the MySQL query logs, it seems that the next iteration of the loop is executed before the function writeTransactionSync()
in the previous loop has finished.
If Meteor._wrapAsync
does not make the function blocking, how can we make the function synchronous?
Server-side Code
writeTransaction = function(data, callback) {
var mysql = Meteor.require('mysql')
var connection = mysql.createConnection(info).connect()
connection.beginTransaction(Meteor.bindEnvironment(function(err) {
connection.query('DELETE FROM orders WHERE _id = ?', [data._id], Meteor.bindEnvironment( function(err) {
connection.commit( Meteor.bindEnvironment( function(err) {
}))
}))
}))
callback(null)
}
writeTransactionSync = Meteor._wrapAsync(writeTransaction)
for(var i=0; i<5; i++) {
writeTransactionSync(data[i])
}
MySQL Query Log
329 Connect root@localhost on meteor
330 Connect root@localhost on meteor
331 Connect root@localhost on meteor
332 Connect root@localhost on meteor
333 Connect root@localhost on meteor
329 Query START TRANSACTION
330 Query START TRANSACTION
331 Query START TRANSACTION
332 Query START TRANSACTION
333 Query START TRANSACTION
329 Query DELETE FROM orders WHERE _id = '34zCYZXBxEkJapkYh'
330 Query DELETE FROM orders WHERE _id = 'SNR8zTEzGCw6X7RZ2'
331 Query DELETE FROM orders WHERE _id = 'TAF2TJkN5LzFRqAnX'
332 Query DELETE FROM orders WHERE _id = '57pJbvFYmHTpM5E6a'
333 Query DELETE FROM orders WHERE _id = 'BtNLGa3gjRGAfmMFf'
331 Query COMMIT
332 Query COMMIT
329 Query COMMIT
330 Query COMMIT
333 Query COMMIT
Copyright Notice:Content Author:「Nyxynyx」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21644692/meteor-wrapasync