Created
April 19, 2020 10:45
-
-
Save zaetrik/bf2f71fda65ed3bdb2bf761f826fb171 to your computer and use it in GitHub Desktop.
Functional JS API Requests -Get Exchange Rates
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
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