[API reference](https://evolu.dev/docs/api-reference) › [@evolu/common](https://evolu.dev/docs/api-reference/common) › [Callbacks](https://evolu.dev/docs/api-reference/common/Callbacks) › Callbacks

Defined in: [packages/common/src/Callbacks.ts:66](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Callbacks.ts#L66)

Request-response correlation for callbacks across boundaries.

Stores callbacks with unique IDs and executes them once with an optional
argument. Executed callbacks are automatically removed.

This is useful for correlating asynchronous request-response operations
across boundaries where callback functions cannot be passed directly (e.g.,
web workers, message queues).

The `execute` method intentionally does not use try-catch or [Result](https://evolu.dev/docs/api-reference/common/Result/type-aliases/Result)
because it's the callback's responsibility to handle its own errors.

### Correlating callback responses

```ts

const deps = testCreateDeps();

// No-argument callback
using callbacks = createCallbacks(deps);
let noArgumentCalls = 0;
const noArgumentId = callbacks.register(() => {
  noArgumentCalls++;
});
callbacks.execute(noArgumentId);
callbacks.execute(noArgumentId);
assertEqual(noArgumentCalls, 1);

// Typed callback
using stringCallbacks = createCallbacks<string>(deps);
let received = "";
const stringCallbackId = stringCallbacks.register((value) => {
  received = value;
});
stringCallbacks.execute(stringCallbackId, "hello");
assertEqual(received, "hello");

// Promise.withResolvers
using promiseCallbacks = createCallbacks<string>(deps);
const { promise, resolve } = Promise.withResolvers<string>();
const promiseCallbackId = promiseCallbacks.register(resolve);
promiseCallbacks.execute(promiseCallbackId, "resolved value");
assertEqual(await promise, "resolved value");
```

## Extends

- [`Disposable`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management)

## Methods

<a id="dispose"></a>

### \[dispose\]()

```ts
dispose: void;
```

Defined in: node\_modules/@typescript/old/lib/lib.esnext.disposable.d.ts:34

#### Inherited from

```ts
Disposable.[dispose]
```

## Properties

<a id="execute"></a>

### execute

```ts
readonly execute: T extends undefined ? (id: string & Brand<"Id">) => undefined : (id: string & Brand<"Id">, arg: T) => undefined;
```

Defined in: [packages/common/src/Callbacks.ts:71](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Callbacks.ts#L71)

Executes and removes a callback associated with the given ID.

---

<a id="register"></a>

### register

```ts
readonly register: (callback: Callback<T>) => string & Brand<"Id">;
```

Defined in: [packages/common/src/Callbacks.ts:68](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Callbacks.ts#L68)

Registers a callback function and returns a unique ID.