Last active
July 4, 2016 08:07
-
-
Save detj/9227048 to your computer and use it in GitHub Desktop.
Deferred returns inside try/finally blocks
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
/** | |
* 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'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run it multiple times