Skip to content

Instantly share code, notes, and snippets.

@Way
Created August 9, 2015 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Way/e3810f68976915de8ec7 to your computer and use it in GitHub Desktop.
Save Way/e3810f68976915de8ec7 to your computer and use it in GitHub Desktop.
Error handling for JSON.parse with lodash
// 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