When adopting Effect into an existing codebase, it's often best to start with discrete, self-contained pieces of code. You can then use features of Effect to easily interop with your existing code and gradually continue to adopt Effect at your own pace.
One of the easiest ways to get started is by replacing Promise
-based code with Effect. This allows you to immediately benefit from the features of Effect, such as detailed error tracking and interruption.
To learn more about Effect vs. Promises, checkout the corresponding FAQ page in the documentation
Effect vs. Promise
Let's say that in our application we have a function getMessage
which takes a numeric id
as a parameter and returns a Promise<string>
. We want to re-implement all the logic inside getMessage
with Effect.
ts
declare functiongetMessage (id : number):Promise <string>async functionmain () {constresult = awaitgetMessage (1)}
ts
declare functiongetMessage (id : number):Promise <string>async functionmain () {constresult = awaitgetMessage (1)}
Because Effect has excellent interop with Promise
-based code, you can freely re-factor getMessage
to use Effect and use Effect.runPromise
elsewhere in your application where getMessage
is await
ed.
ts
import {Effect } from "effect"declare functiongetMessage (id : number):Effect .Effect <string>async functionmain () {constresult = awaitEffect .runPromise (getMessage (1))}
ts
import {Effect } from "effect"declare functiongetMessage (id : number):Effect .Effect <string>async functionmain () {constresult = awaitEffect .runPromise (getMessage (1))}
The team in charge of the TodoRepository
has re-factored the create
method. The method now returns an Effect<Todo>
instead of a Promise<Todo>
.
Using what we have learned above, your tasks for this exercise include:
POST /todos
route of our Express APITodo
as JSONModify the code in the editor to the right to accomplish your tasks.