Docs
Running Effects

Running Effects

To run an Effect, we can use the variety of run functions available on the Effect module:

ts
import * as Effect from "@effect/io/Effect"
 
// sync variant
export const programSync = Effect.runSync(Effect.succeed("Hello, World!"))
const programSync: string
 
// promise variant
export const programPromise = Effect.runPromise(
const programPromise: Promise<string>
Effect.succeed("Hello, World!")
)
 
// promise Either variant
export const programPromiseEither = Effect.runPromiseEither(
const programPromiseEither: Promise<Either<never, string>>
Effect.succeed("Hello, World!")
)
 
// callback variant
export const programCallback = Effect.runCallback(
const programCallback: Cancel<never, string>
Effect.succeed("Hello, World!"),
(exit) => console.log("I'm done!")
)
ts
import * as Effect from "@effect/io/Effect"
 
// sync variant
export const programSync = Effect.runSync(Effect.succeed("Hello, World!"))
const programSync: string
 
// promise variant
export const programPromise = Effect.runPromise(
const programPromise: Promise<string>
Effect.succeed("Hello, World!")
)
 
// promise Either variant
export const programPromiseEither = Effect.runPromiseEither(
const programPromiseEither: Promise<Either<never, string>>
Effect.succeed("Hello, World!")
)
 
// callback variant
export const programCallback = Effect.runCallback(
const programCallback: Cancel<never, string>
Effect.succeed("Hello, World!"),
(exit) => console.log("I'm done!")
)

runSync should only be used if your Effect doesn't perform any asynchronous tasks. If you're not sure, you can try using it, and it will crash at runtime with a noticeable error if you should be using one of the other run methods instead.

There's several more run functions available on the Effect module, but these are the some common ones you may use.

The ideal way to work with Effects is to have as much of your program as an Effect as possible. You should try to use the run functions as close to the "edge" of your program as possible. This will allow you to have the most flexibility in how you run your program and build up very sophisticated Effects.