Created
August 9, 2015 19:34
-
-
Save Way/e3810f68976915de8ec7 to your computer and use it in GitHub Desktop.
Error handling for JSON.parse with lodash
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
// With Lodash | |
function parseLodash(str){ | |
return _.attempt(JSON.parse.bind(null, str)); | |
} | |
parse('a'); // false | |
parseLodash('a'); // Return an error object | |
parse('{"name": "colin"}'); // Return {"name": "colin"} | |
parseLodash('{"name": "colin"}'); // Return {"name": "colin"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment