Running Effects
To run an Effect
, we can use the variety of run
functions available on the Effect
module:
ts
import * asEffect from "@effect/io/Effect"// sync variantexport constprogramSync =Effect .runSync (Effect .succeed ("Hello, World!"))// promise variantexport constprogramPromise =Effect .runPromise (Effect .succeed ("Hello, World!"))// promise Either variantexport constprogramPromiseEither =Effect .runPromiseEither (Effect .succeed ("Hello, World!"))// callback variantexport constprogramCallback =Effect .runCallback (Effect .succeed ("Hello, World!"),(exit ) =>console .log ("I'm done!"))
ts
import * asEffect from "@effect/io/Effect"// sync variantexport constprogramSync =Effect .runSync (Effect .succeed ("Hello, World!"))// promise variantexport constprogramPromise =Effect .runPromise (Effect .succeed ("Hello, World!"))// promise Either variantexport constprogramPromiseEither =Effect .runPromiseEither (Effect .succeed ("Hello, World!"))// callback variantexport constprogramCallback =Effect .runCallback (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.