Skip to content

Instantly share code, notes, and snippets.

@detj
Last active July 4, 2016 08:07
Show Gist options
  • Save detj/9227048 to your computer and use it in GitHub Desktop.
Save detj/9227048 to your computer and use it in GitHub Desktop.
Deferred returns inside try/finally blocks
/**
* Return inside try/finally
*/
// THIS IS JUST A TEST CASE
// AVOID USING SUCH CONSTRUCTS IN PRODUCTION CODE
// foo is either 0 or 1
var foo = Math.round(Math.random() * 10) % 2;
console.log(bar());
function bar() {
try {
if (0 === foo) {
// return is deferred
return '0';
} else if (1 === foo) {
// return is deferred
return '1';
}
} catch(e) {
} finally {
console.log('in finally');
// Uncomment this return & it hijacks other returns
// return 'finally';
}
}
@detj
Copy link
Author

detj commented Feb 26, 2014

To run it multiple times

$ seq 1 20 | xargs -Iz node try.js

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