Skip to content

Instantly share code, notes, and snippets.

@zaetrik
Created April 19, 2020 10:45
Show Gist options
  • Save zaetrik/bf2f71fda65ed3bdb2bf761f826fb171 to your computer and use it in GitHub Desktop.
Save zaetrik/bf2f71fda65ed3bdb2bf761f826fb171 to your computer and use it in GitHub Desktop.
Functional JS API Requests -Get Exchange Rates
const getExchangeRatesForCountry = (countryCode: CountryCode) =>
pipe(
// tryCatch transforms our Promise that may reject to a Promise that never rejects and returns an Either instead
taskEither.tryCatch(
// We need an anonymous function here because we can only use tryCatch with functions that return Lazy<Promise<any>>
() => fetchExchangeRatesForCountry(countryCode),
either.toError
),
taskEither.chain(
// flow => function composition (from left to right)
// could also be written without flow() as taskEither.fromEither(validateExchangeRates(countryCode))
// we validate our response
// => returns Either
// => transform Either to TaskEither
flow(validateExchangeRates(countryCode), taskEither.fromEither)
)
)();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment