Update example for nested pipes

This commit is contained in:
Scott Picquerey 2022-04-06 15:26:42 +02:00
parent 6fe9e04f45
commit e056cdc02c

View file

@ -78,37 +78,64 @@ const triggerEmailCampaign = ({
```typescript ```typescript
// Bad // Bad
const refreshUserToken = (user: User) => const convertDollarAmountInCountryCurrency = ({
countryName,
amountInDollar,
}: {
countryName: CountryName;
amountInDollar: number;
}) =>
pipe( pipe(
user.token, getCountryCode(countryName),
option.match( either.map(
() => AuthClient.createToken(user), countryCode =>
(token) =>
pipe( pipe(
AuthClient.refreshToken({ user, token }), getCountryCurrency(countryCode),
rte.chainW(({ newToken }) => option.map(currency =>
pipe(newToken.hash, user.updateToken, rte.chainEitherKW(storeUser)), pipe(
amountInDollar,
converFromDollar(currency),
convertedAmount =>
console.log(
`converted amount for country ${countryCode} is ${convertedAmount}`,
),
),
),
), ),
), ),
), ),
); );
// Good // Good
const updateUserToken = (user: User) => const convertDollarAmountInCountryCodeCurrency = ({
flow(user.updateToken, rte.chainEitherKW(storeUser)); amountInDollar,
countryCode,
const refreshAndUpdateUserToken = (user: User) => (token: Token) => }: {
amountInDollar: number;
countryCode: CountryCode;
}) =>
pipe( pipe(
AuthClient.refreshToken({ user, token }), getCurrencyFromCountryCode(countryCode),
rte.chainW(({ newToken }) => updateUserToken(user)(newToken.hash)), option.map(currency => {
const convertedDollarAmount = convertFromDollar(currency)(currency);
console.log(
`converted amount for country ${countryCode} is ${convertedAmount}`,
);
}),
); );
const refreshUserToken = (user: User) => const convertDollarAmountInCountryCurrency = ({
amountInDollar,
countryName,
}: {
amountInDollar: number;
countryName: CountryName;
}) =>
pipe( pipe(
user.token, getCountryCode(countryName),
option.match( either.map(countryCode =>
() => AuthClient.createToken(user), convertDollarAmountToCountryCodeCurrency({ amountInDollar, countryCode }),
renewAndUpdateUserToken(user),
), ),
); );
``` ```