TutorialsAlpha

Getting Started

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

Refactoring from 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 function getMessage(id: number): Promise<string>
 
async function main() {
const result = await getMessage(1)
}
ts
declare function getMessage(id: number): Promise<string>
 
async function main() {
const result = await getMessage(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 awaited.

ts
import { Effect } from "effect"
 
declare function getMessage(id: number): Effect.Effect<string>
 
async function main() {
const result = await Effect.runPromise(getMessage(1))
}
ts
import { Effect } from "effect"
 
declare function getMessage(id: number): Effect.Effect<string>
 
async function main() {
const result = await Effect.runPromise(getMessage(1))
}

Exercise

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:

  • Fix all type errors within the POST /todos route of our Express API
  • Ensure that the Express API responds with the created Todo as JSON

Modify the code in the editor to the right to accomplish your tasks.

Next: Dealing with Errors