Skip to content

Instantly share code, notes, and snippets.

@veeramarni
Forked from cmather/async.js
Created December 11, 2015 22:47
Show Gist options
  • Save veeramarni/be578ec907723b13899e to your computer and use it in GitHub Desktop.
Save veeramarni/be578ec907723b13899e 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);
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment