usefulio:sync-methods

v0.2.0Published 8 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Note: 0.1.x branch supports Meteor < 1.3 0.2.x branch supports Meteor 1.3.x+

Meteor.syncMethods

If you want to call a Meteor method using Meteor.call or Meteor.apply from the client, but the result of that call relies on an asychronous action on the server... you can use Meteor.syncMethods(...) instead of Meteor.methods(...) to return the result to the client only after the asychronous action has returned.

This is non-blocking on the client or the server side for subsequent requests because of Meteor's use of fibers.

How to use

1Meteor.syncMethods({
2  myMethodOne: function(myArg1, myArg2, callback){
3    // do some async stuff
4    callback(err, result);
5  }
6  , myMethodTwo: function(callback){
7    callback(err, result);
8  }
9});
10

All functions registered via Meteor.syncMethods will be passed a callback function as their last parameter automatically. When you are done with your asynchronous action, simply call the callback passing in any error and result.

If the error is not undefined, it will be thrown and transmitted back to the client.

If the error is undefined the result will be returned to the client calling the method just as if you had done return result; in a normal Meteor.methods({...}).

License

MIT