Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Created June 22, 2019 04:24
Show Gist options
  • Save jsmanifest/9a258f7fdd5a356df65cbe2dbdc5f02c to your computer and use it in GitHub Desktop.
Save jsmanifest/9a258f7fdd5a356df65cbe2dbdc5f02c to your computer and use it in GitHub Desktop.
import axios from 'axios'
const getFrogs = async (params) => {
try {
const frogs = await axios.get('https://frogs-and-their-tongues.com', params)
return data
} catch (error) {
throw error
}
}
const calcAverageWidthOfTongues = async (params) => {
try {
const frogs = await getFrogs(params)
const tongueWidths = frogs.reduce((sum, frog) => {
return sum + frog.tongueWidth
}, 0)
const averageWidth = tongueWidths / frogs.length
return averageWidth
} catch (error) {
throw error
}
}
const useTongueObj = (fn, options) => {
return async (params) => {
const newParams = { ...params }
if (options.limit !== undefined) {
newParams.limit = options.limit
}
let averageWidth = await fn(newParams)
if (typeof options.multiplyBy === 'number') {
averageWidth = averageWidth * options.multiplyBy
}
return {
averageWidth,
multiplied: typeof options.multiplyBy === 'number',
size: averageWidth < 2 ? 'small' : 'large', // size in inches
}
}
}
const calcTongueWidths = useTongueObj(calcAverageWidthOfTongues, {
multiplyBy: 2,
})
calcTongueWidths({ limit: 10 })
.then((tongueObj) => {
console.log(tongueObj)
/*
result:
{
averageWidth: 8,
multiplied: true,
size: 'large'
}
*/
})
.catch((error) => {
console.log(result)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment