Skip to content

Thenables Support #1

@ohenrydev

Description

@ohenrydev

Main context

Improve $trycatch to support thenables (non-Promise awaitables)

Description

Currently, $trycatch only treats values as asynchronous if they are instances of Promise. This means that objects which implement a .then() method (thenables), such as those returned by Prisma ORM methods (prisma.users.findAll()), are not handled as async results even though they are awaitable.

Prisma client methods and similar libraries often return thenables which work with await, but fail the instanceof Promise check. This leads to $trycatch treating them as synchronous values, which can cause unexpected behavior or missed error handling.

Steps to reproduce

const result = $trycatch(prisma.users.findAll());
// result is treated as a synchronous TryCatch, not a Promise<TryCatch>

Suggested solution

Instead of using instanceof Promise, check if the value is an object and has a .then method of type "function", as recommended in thenable detection patterns.

if (
  maybePromiseResult &&
  typeof maybePromiseResult === "object" &&
  typeof maybePromiseResult.then === "function"
) {
  return Promise.resolve(maybePromiseResult).then(ok).catch(err);
}

This will ensure $trycatch works correctly with all awaitables, including thenables, and matches the behavior of await.

Acceptance Criteria

  • $trycatch should handle any value with a .then method as an asynchronous result.
  • Update documentation to mention thenable support.
  • Add tests for thenables (e.g. Prisma client return values).

References

Additional context

Supporting thenables will improve compatibility with libraries like Prisma ORM and any custom awaitable implementations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions