<A, E>(effect: Effect<A, E>): AExecutes an effect synchronously and returns its success value.
When to use
Use when you need to execute an effect that is guaranteed to complete synchronously.
Details
If the effect fails, dies, is interrupted, or performs asynchronous work,
runSync throws a FiberFailure instead of returning a value. Use
runSyncExit when you want the failure captured as an Exit.
Example (Running a synchronous effect)
import { Effect } from "effect"
const program = Effect.sync(() => {
console.log("Hello, World!")
return 1
})
const result = Effect.runSync(program)
// Output: Hello, World!
console.log(result)
// Output: 1Example (Throwing for failed or async effects)
import { Effect } from "effect"
try {
// Attempt to run an effect that fails
Effect.runSync(Effect.fail("my error"))
} catch (e) {
console.error(e)
}
// Output:
// (FiberFailure) Error: my error
try {
// Attempt to run an effect that involves async work
Effect.runSync(Effect.promise(() => Promise.resolve(1)))
} catch (e) {
console.error(e)
}
// Output:
// (FiberFailure) AsyncFiberException: Fiber #0 cannot be resolved synchronously. This is caused by using runSync on an effect that performs async workexport const const runSync: <A, E>(
effect: Effect<A, E>
) => A
Executes an effect synchronously and returns its success value.
When to use
Use when you need to execute an effect that is guaranteed to complete
synchronously.
Details
If the effect fails, dies, is interrupted, or performs asynchronous work,
runSync throws a FiberFailure instead of returning a value. Use
runSyncExit when you want the failure captured as an Exit.
Example (Running a synchronous effect)
import { Effect } from "effect"
const program = Effect.sync(() => {
console.log("Hello, World!")
return 1
})
const result = Effect.runSync(program)
// Output: Hello, World!
console.log(result)
// Output: 1
Example (Throwing for failed or async effects)
import { Effect } from "effect"
try {
// Attempt to run an effect that fails
Effect.runSync(Effect.fail("my error"))
} catch (e) {
console.error(e)
}
// Output:
// (FiberFailure) Error: my error
try {
// Attempt to run an effect that involves async work
Effect.runSync(Effect.promise(() => Promise.resolve(1)))
} catch (e) {
console.error(e)
}
// Output:
// (FiberFailure) AsyncFiberException: Fiber #0 cannot be resolved synchronously. This is caused by using runSync on an effect that performs async work
runSync: <function (type parameter) A in <A, E>(effect: Effect<A, E>): AA, function (type parameter) E in <A, E>(effect: Effect<A, E>): AE>(effect: Effect<A, E>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect: interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <A, E>(effect: Effect<A, E>): AA, function (type parameter) E in <A, E>(effect: Effect<A, E>): AE>) => function (type parameter) A in <A, E>(effect: Effect<A, E>): AA = import internalinternal.const runSync: <A, E>(
effect: Effect.Effect<A, E>
) => A
runSync