Created
February 1, 2012 15:19
-
-
Save bennadel/1717494 to your computer and use it in GitHub Desktop.
Use A Return Statement When Invoking Callbacks, Especially In A Guard Statement
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Using Return Statements When Invoking Callbacks</title> | |
<script type="text/javascript"> | |
// Define a success handler. | |
var successHandler = function(){ | |
console.log( "Success!" ); | |
}; | |
// Define a fail handler. | |
var failHandler = function(){ | |
console.log( "Fail :(" ); | |
}; | |
// Now, let's create a mock function that makes use of the | |
// success and fail callback handlers. | |
var doSomething = function( successHandler, failHandler ){ | |
// Check to see if some value is true - for a mock | |
// fail handler invocation. | |
if (true === true){ | |
return( failHandler() ); | |
} | |
// If we made it this far, then everything executed | |
// well - invoke success handler. | |
return( successHandler() ); | |
}; | |
/// Invoke the callback-oriented method. | |
doSomething( successHandler, failHandler ); | |
</script> | |
</head> | |
<body> | |
<!-- Left intentionally blank. --> | |
</body> | |
</html> |
This file contains hidden or 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
return( failHandler() ); |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Using Return Statements When Invoking Callbacks</title> | |
<script type="text/javascript"> | |
// Define a success handler. | |
var successHandler = function(){ | |
console.log( "Success!" ); | |
}; | |
// Define a fail handler. | |
var failHandler = function(){ | |
console.log( "Fail :(" ); | |
}; | |
// Now, let's create a mock function that makes use of the | |
// success and fail callback handlers. | |
var doSomething = function( successHandler, failHandler ){ | |
// Check to see if some value is true - for a mock | |
// fail handler invocation. | |
if (true === true){ | |
failHandler(); | |
} | |
// If we made it this far, then everything executed | |
// well - invoke success handler. | |
successHandler(); | |
}; | |
/// Invoke the callback-oriented method. | |
doSomething( successHandler, failHandler ); | |
</script> | |
</head> | |
<body> | |
<!-- Left intentionally blank. --> | |
</body> | |
</html> |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Using Return Statements In Guard Logic</title> | |
<script type="text/javascript"> | |
// A simple method including guard logic. | |
var doSomething = function(){ | |
// Check some guard logic. | |
if (true === true){ | |
// Return out of function exection since some | |
// condition indicates that the rest of the | |
// method should not be invoked. | |
return; | |
} | |
// If we made it this far, all of the guard logic | |
// indicated that we are good to go on execution. | |
return; | |
}; | |
// Invoke our guarded method. | |
doSomething(); | |
</script> | |
</head> | |
<body> | |
<!-- Left intentionally blank. --> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment