Meteor._wrapAsync
NickName:Nyxynyx Ask DateTime:2014-02-08T18:10:33

Meteor._wrapAsync

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

Answers
Tarang 2014-02-08T10:16:05

Meteor._wrapAsync is synchronous, provided that you fire the callback after all the tasks are done. It will only block the fiber if it knows the method has completed, so when you call callback(null), it assumes it is complete.\n\nYou have used callback(null) after you call connection.query but it would be called regardless of whether your query has completed or not, and since it takes a bit of time for a query to execute with MySQL, its likely its always complete (and act asynchronous) before the query has actually run.\n\nYou should place it inside the callback of the query:\n\nconnection.beginTransaction(function(err) {\n connection.query('DELETE FROM orders WHERE _id = ?', [data._id], function(err) {\n connection.commit(function(err) {\n callback(null);\n });\n });\n});\n\n\nAlso you don't need Meteor.bindEnvironment when you use Meteor._wrapAsync, especially when you're not using any Meteor code in the functions anywhere (You dont use meteor code in any of your connection callbacks, so its not needed).",


More about “Meteor._wrapAsync” related questions

Meteor._wrapAsync

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 itera...

Show Detail

Meteor._wrapAsync causes 100% CPU

An app is using meteor-redis package to run a redis query which can sometimes take 30 seconds to return 100k results. During this waiting time, Meteor freezes and takes up 100% CPU while waiting fo...

Show Detail

Meteor._wrapAsync causes Meteor to be unresponsive

I'm using Meteor._wrapAsync to force only one call to the function writeMeLater to be executing at any one time. If 10 calls to writeMeLater are made within 1 second, the other 9 calls should be q...

Show Detail

Fully block asynchronous calls in nodejs like Meteor._wrapAsync

I have the following code in Meteor (nodejs framework) dbAllSync = Meteor._wrapAsync(db.all.bind(db)) rows = dbAllSync(query) With the above code I am able to fully block the db call i.e. the code

Show Detail

Meteor._wrapAsync and node child processes

I try to get results from child_process commands in Meteor environment. It seems that there is something special in child_process, which I don't understand. Here is the code I used for testing Met...

Show Detail

Meteor._wrapAsync has been renamed to Meteor.wrapAsync undefined

I have been getting the warning on meteor server start-up. Meteor._wrapAsync has been renamed to Meteor.wrapAsync undefined. Does anyone have any idea why?

Show Detail

Meteor._wrapAsync doesn't seem to work with stripe api library

When I use _wrapAsync like so: var stripeChargeCreate = Meteor._wrapAsync(_.bind(Stripe.charges.create,Stripe)); var charge = stripeChargeCreate({ amount: amount, currency: "usd", card:

Show Detail

Why I can't use sinon.useFakeTimers and Meteor._wrapAsync

I try to use sinon.useFakeTimers on some tests to make a bunch of setTimeout fire and this works fine, except when I call any function that is wrapped with Meteor._wrapAsync. I've tried using Asycn...

Show Detail