Hyperlinkv0.8.0-beta.28

Effect

Effect.runSyncconsteffect/Effect.ts:9219
<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
Source effect/Effect.ts:92191 lines
export const runSync: <A, E>(effect: Effect<A, E>) => A = internal.runSync
Referenced by 2 symbols