Skip to content

Instantly share code, notes, and snippets.

@ybogdanov
Created March 25, 2011 10:21
Show Gist options
  • Save ybogdanov/886651 to your computer and use it in GitHub Desktop.
Save ybogdanov/886651 to your computer and use it in GitHub Desktop.
Example that shows how you can easily integrate node-sync to your app without refactoring
someObject = {
myNewMethod : function(a, b) // <-- no callback
{
console.log(Fiber.current); // Inside of fiber
if (a == 1)
throw "'a' cannot be 1"; // we can use throw here
return a + b; // the result as regular 'return' value
}.async(), // <-- make it friendly with async environment
myOtherNewMethod : function()
{
console.log(Fiber.current); // Inside of fiber
// we can use myNewMethod synchronously from here
var result = this.myNewMethod(2, 2);
console.log(result); // 4
// it will throw exceptions right here
try {
var result = this.myNewMethod(1, 2);
}
catch (e) {
console.log(e); // 'a' cannot be 1
}
}.async()
}
// My asynchronously app which doesn't know anything about fibers
MyApp(function(){
// So, we can use myNewMethod as a regular asynchronous function
someObject.myNewMethod(2, 2, function(err, result){
console.log(result); // 4
})
// Errors as it should be
someObject.myNewMethod(1, 2, function(err, result){
console.log(err); // 'a' cannot be 1
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment