Skip to content

recipher-js: chain Result methods on a pending call, so await foo().unwrap() works without parentheses #2026

Backlog

Description

@coderdan

Background

@cipherstash/recipher is the TypeScript SDK on Stack Encrypt, in languages/typescript/packages/recipher-js. Every call returns, or resolves to, a Result, with the methods of Rust's Result in camelCase. Only unwrap and expect throw (README, "Results, not exceptions").

Problem

An async call needs parentheses before any Result method, because await applies to the whole expression after it:

const cipher = (await Cipher.create()).unwrap()
const stored = (await keyset.encrypt(user, { record: UserRecord })).unwrap()

Rust chains .await like a method. TypeScript has no such form, so the Rust pattern needs the parentheses here. The parentheses make each call harder to read at a glance.

Proposal

An async call returns a pending result: an object with then, and the Result methods that do not need the value before it arrives. The neverthrow library calls this ResultAsync.

const cipher = await Cipher.create().unwrap()
const stored = await keyset.encrypt(user, { record: UserRecord }).unwrap()
const age = await keyset.encrypt(34n, { kind: kinds.uint64 }).map(toBytes).unwrapOr(empty)
const result = await keyset.encrypt(user, { record: UserRecord }) // still a Result
  1. A thenable class, not a Promise subclass. await and Promise.all accept any thenable, and a subclass of Promise changes what then returns.
  2. The methods: map, mapErr, andThen, orElse, unwrap, unwrapOr, expect, and match. Each returns a pending result or a promise. isOk and isErr are left out, because they cannot be answered before the value arrives.
  3. The rule does not change. await x resolves to a Result and never rejects. Only unwrap and expect reject, on an Err, as they throw today.
  4. Synchronous calls keep returning a plain Result: Ciphertext.fromBytes, RcDate.from, and the other factories.
  5. The language binding returns it. No TypeScript code wraps a native method: each method returns its Result itself. The package's TypeScript hands the binding the pending-result class when it loads the binding, as it hands over Ok and Err today. The methods that set up a call (.audit, .withCredential, .signal) are on the same object, and come before any Result method.
  6. README, examples, and type tests are updated, including the "Results, not exceptions" section.

Relationship to other work

Activity

  1. added
    enhancement
    JavaScript
    on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    JavaScriptSDKenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions