Skip to content

Instantly share code, notes, and snippets.

@hungneox
Last active February 21, 2022 17:29
Show Gist options
  • Save hungneox/50da90fcef5c95f297f428e9a0f6b700 to your computer and use it in GitHub Desktop.
Save hungneox/50da90fcef5c95f297f428e9a0f6b700 to your computer and use it in GitHub Desktop.
import * as TE from "fp-ts/TaskEither";
import { pipe } from "fp-ts/function";
const createUser = (username: string): TE.TaskEither<Error, string> => {
return TE.right(`UserId-${username}`);
};
const createOrder = (userId: string): TE.TaskEither<Error, string> => {
return TE.right(`Order-${userId}`);
};
const createOrderRow = (
orderId: string,
userId: string
): TE.TaskEither<Error, string> => {
return TE.right(`OrderRowFor-${orderId}-${userId}`);
};
const main = pipe(
TE.Do,
TE.bind("userId", () => createUser("Rick")),
TE.bind("orderId", ({ userId }) => createOrder(userId)),
TE.bind("orderRowId", ({ userId, orderId }) =>
createOrderRow(userId, orderId)
),
TE.map(({ userId, orderId, orderRowId }) => ({
userId,
orderId,
orderRowId,
}))
);
// The function above will return something likes
// {
// _tag: 'Right',
// right: {
// userId: 'UserIdRick',
// orderId: 'Order123456-UserIdRick',
// orderRowId: 'OrderRowFor-UserIdRick-Order123456-UserIdRick'
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment