Skip to content

Instantly share code, notes, and snippets.

@andyhd
Created January 23, 2012 11:25
Show Gist options
  • Save andyhd/1662627 to your computer and use it in GitHub Desktop.
Save andyhd/1662627 to your computer and use it in GitHub Desktop.
Handling exceptions in mapped function
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