Skip to content

Instantly share code, notes, and snippets.

@electerious
Last active July 30, 2016 14:47
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 electerious/628ef7a18207166999576b64f8c88e14 to your computer and use it in GitHub Desktop.
Save electerious/628ef7a18207166999576b64f8c88e14 to your computer and use it in GitHub Desktop.
Try to execute a function and discard any error that occurs
const nicetry = (fn) => {
try { return fn() }
catch (e) {}
}
@electerious
Copy link
Author

electerious commented Jun 30, 2016

Example:

nicetry(() => JSON.parse('true')) // true
nicetry(() => JSON.parse('truee')) // undefined

nicetry(JSON.parse.bind(this, 'true')) // true

nicetry() // undefined
nicetry(true) // undefined

@electerious
Copy link
Author

Moved to nice-try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment