Created
February 13, 2019 19:03
-
-
Save jq-87/b54dfa8a3c10ce8ecc0c4408cfd8ee13 to your computer and use it in GitHub Desktop.
safely get an object property
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
const tryFn = (fn, fallback = null) => { | |
try { | |
return fn() | |
} catch (error) { | |
return fallback | |
} | |
} | |
const favoriteBook = tryFn(() => favorites.reading.book[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment