Skip to content

Op

Op<I, E, A> = object

Defined in: Core/Op.ts:64

A reusable description of async work — decoupled from execution strategy and lifetime.

Separate concerns:

  • What to do: encoded in the Op via Op.create
  • How to execute: chosen at Op.interpret time (restartable, exclusive, queue, etc.)

An Op never runs on its own. It only executes when passed to Op.interpret, which attaches a concurrency strategy and returns a Manager that owns the execution.

I

E

A

const fetchUser = Op.create(
  (signal) => (id: string) =>
    fetch(`/users/${id}`, { signal }).then(r => {
      if (!r.ok) throw new Error(`${r.status} ${r.statusText}`);
      return r.json() as Promise<User>;
    }),
  (e) => new ApiError(e),
);

const manager = Op.interpret(fetchUser, { strategy: "restartable" });
manager.subscribe(state => {
  if (state.kind === "Pending") showSpinner();
  if (state.kind === "Ok")      render(state.value);
  if (state.kind === "Err")     showError(state.error);
  if (state.kind === "Nil")     resetUI();
});
manager.run(userId);