Hyperlinkv0.8.0-beta.28

Effect

Effect.promiseconsteffect/Effect.ts:869
<A>(evaluate: (signal: AbortSignal) => PromiseLike<A>): Effect<A>

Creates an Effect that represents an asynchronous computation guaranteed to succeed.

When to use

Use to convert a Promise into an Effect when the async operation is guaranteed to succeed and will not reject.

Details

An optional AbortSignal can be provided to allow for interruption of the wrapped Promise API.

Gotchas

The Promise must not reject. If it rejects, the rejection is treated as a defect, not as a typed failure. Use tryPromise when rejection is expected.

Interruption aborts the provided AbortSignal, but the underlying asynchronous operation only stops if it observes that signal.

Example (Wrapping a non-rejecting Promise)

import { Effect } from "effect"

const delay = (message: string) =>
  Effect.promise<string>(
    () =>
      new Promise((resolve) => {
        setTimeout(() => {
          resolve(message)
        }, 2000)
      })
  )

//      ┌─── Effect<string, never, never>
//      ▼
const program = delay("Async operation completed successfully!")
constructorstryPromise
Source effect/Effect.ts:8693 lines
export const promise: <A>(
  evaluate: (signal: AbortSignal) => PromiseLike<A>
) => Effect<A> = internal.promise
Referenced by 9 symbols