Last active
February 21, 2022 17:29
-
-
Save hungneox/50da90fcef5c95f297f428e9a0f6b700 to your computer and use it in GitHub Desktop.
This file contains 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 * 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