Created
June 22, 2019 04:24
-
-
Save jsmanifest/9a258f7fdd5a356df65cbe2dbdc5f02c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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