Attempt
Replaces lodash.get
with a similar API while also providing additional features.
Why?
Lodash is an amazing utility however it's quite bloated, even its modularized version.
This module has around 0.2kb
while lodash.get
has 27kb
.
Remember that size is really important in our serverless world :rocket:
API
attempt(getter, fallback)
getter
: A function which returns the value we want or executes another functionfallback
: Fallback value in case getter
throws or returns undefined
. If typeof fallback === 'function'
then it will execute it.
Usage
Retrieving data based on a given path:
const data = attempt(() => response.items[2].payload)
const dataWithFallback = attempt(() => response.items[2].payload, null)