Created
July 20, 2013 23:14
-
-
Save cmather/6046790 to your computer and use it in GitHub Desktop.
Async call from within a Meteor server side method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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?