Created
January 23, 2012 11:25
-
-
Save andyhd/1662627 to your computer and use it in GitHub Desktop.
Handling exceptions in mapped function
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
function tryo(fn) { | |
var res = null; | |
try { | |
res = fn(); | |
} catch (e) { | |
res = null; | |
} | |
return maybe(res); | |
} | |
function thrower(x) { | |
if (x == 2) throw new Error("An exception"); | |
return x; | |
} | |
console.log([1, 2, 3].map(function (i) { | |
return tryo(function () { return thrower(i) }).getOrElse("fail"); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment