Skip to content

Instantly share code, notes, and snippets.

@cmather
Created July 20, 2013 23:14
Show Gist options
  • Save cmather/6046790 to your computer and use it in GitHub Desktop.
Save cmather/6046790 to your computer and use it in GitHub Desktop.
Async call from within a Meteor server side method.
if (Meteor.isServer) {
var Fiber = Npm.require('fibers');
function async (cb) {
Meteor.setTimeout(function () {
cb(null, 'hello');
}, 3000);
}
Meteor.methods({
callAsync: function () {
var fiber = Fiber.current;
var fence = Meteor._CurrentWriteFence.get()
, handle = fence && fence.beginWrite();
async(function (err, res) {
handle && handle.committed();
fiber.run(res);
});
return Fiber.yield();
}
});
}
if (Meteor.isClient) {
testCallAsync = function () {
Meteor.call('callAsync', function (err, res) {
if (err) console.log(err);
else console.log('response: ', res);
});
};
}
@iimplicit
Copy link

Hello. Thank you for your great lectures from Evented Mind.

Can I ask you one question?

Looking at your code, what is difference between using fiber(Fiber.current) and future(new Future()) to solve server-side async issues? Is there any preference over another?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment