Hyperlinkv0.8.0-beta.28

Effect

Effect.timeoutconsteffect/Effect.ts:4494
(duration: Duration.Input): <A, E, R>(
  self: Effect<A, E, R>
) => Effect<A, E | Cause.TimeoutError, R>
<A, E, R>(self: Effect<A, E, R>, duration: Duration.Input): Effect<
  A,
  E | Cause.TimeoutError,
  R
>

Adds a time limit to an effect, triggering a timeout if the effect exceeds the duration.

When to use

Use when you need a timeout of an Effect to be represented as a typed failure.

Details

The timeout function allows you to specify a time limit for an effect's execution. If the effect does not complete within the given time, a TimeoutException is raised. This can be useful for controlling how long your program waits for a task to finish, ensuring that it doesn't hang indefinitely if the task takes too long.

Gotchas

If the timeout wins, the source effect is interrupted.

Example (Failing when work takes too long)

import { Effect } from "effect"

const task = Effect.gen(function*() {
  console.log("Start processing...")
  yield* Effect.sleep("2 seconds") // Simulates a delay in processing
  console.log("Processing complete.")
  return "Result"
})

// Output will show a TimeoutException as the task takes longer
// than the specified timeout duration
const timedEffect = task.pipe(Effect.timeout("1 second"))

Effect.runPromiseExit(timedEffect).then(console.log)
// Output:
// Start processing...
// {
//   _id: 'Exit',
//   _tag: 'Failure',
//   cause: {
//     _id: 'Cause',
//     _tag: 'Fail',
//     failure: { _tag: 'TimeoutException' }
//   }
// }
Source effect/Effect.ts:44949 lines
export const timeout: {
  (
    duration: Duration.Input
  ): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E | Cause.TimeoutError, R>
  <A, E, R>(
    self: Effect<A, E, R>,
    duration: Duration.Input
  ): Effect<A, E | Cause.TimeoutError, R>
} = internal.timeout
Referenced by 1 symbols