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 🚀
API
attempt(getter, fallback)
-
getter
: A function which returns the value we want or executes another function -
fallback
: Fallback value in casegetter
throws or returnsundefined
. Iftypeof 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)