Skip to main content

Overview

The fromSafePromise function converts a Promise<T> that you know will never reject into a ResultAsync<T, E>. Unlike fromPromise, it does not require an error handler because it assumes the promise will always resolve successfully. fromSafePromise is a top-level export that references ResultAsync.fromSafePromise.
Use with CautionOnly use this function when you are absolutely certain the promise will not reject. If the promise does reject, the ResultAsync will reject as well, breaking the Result abstraction. When in doubt, use fromPromise instead.

Type Signature

Parameters

  • promise: A PromiseLike<T> that is guaranteed to never reject

Return Value

Returns a ResultAsync<T, E> that resolves to Ok<T> containing the promise’s resolved value.

When to Use

Use fromSafePromise when:
  1. You’re working with Promise.resolve() or similar guaranteed-to-succeed promises
  2. You’ve wrapped a promise in a try-catch that ensures it never rejects
  3. You’re using a library that returns promises but documents they never reject
  4. You want to convert a value to ResultAsync for API consistency

Basic Usage

Converting a Simple Delay

Working with asyncMap

Real-World Examples

Rate Limiting

Simulating Slow Operations

Promise.resolve Wrapper

Debounced Operations

Testing Utilities

Animation Delays

Batch Processing with Delays

Internal Usage

Many Result/ResultAsync methods use fromSafePromise internally:

asyncMap Implementation

Comparison with fromPromise

Using fromPromise (Safe, Verbose)

Using fromSafePromise (Less Safe, Concise)

Type Safety

Common Pitfalls

Don’t Use with Potentially Failing Operations
Don’t Use with Untrusted Promises

Key Points

  • Only use when promise is guaranteed to never reject
  • No error handler required
  • Error type defaults to never
  • Useful for delays, timeouts, and Promise.resolve
  • Used internally by asyncMap and similar methods
  • When in doubt, use fromPromise instead
  • If the promise rejects, the entire ResultAsync abstraction breaks