Meteor._wrapAsync

I wrapped the function writeTransaction()with Meteor._wrapAsyncand called it 5 times in a loop forthat records the MySQL transaction.

However, from the MySQL query logs, it seems that the next iteration of the loop is completed until the function completes writeTransactionSync()in the previous loop.

If it Meteor._wrapAsyncdoes not block the function, 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
+3
source share
2 answers

Meteor._wrapAsyncIt is synchronous provided that you start the callback after all tasks are completed. It will only block the fiber if it knows that the method is complete, so when you call callback(null), it assumes that it is complete.

callback(null) connection.query, , , MySQL , ( ) , .

:

connection.beginTransaction(function(err) {
    connection.query('DELETE FROM orders WHERE _id = ?', [data._id], function(err) {
        connection.commit(function(err) {
            callback(null);
        });
    });
});

Meteor.bindEnvironment, Meteor._wrapAsync, - ( , ).

+4

All Articles