---
url: 'https://adk.nht.io/api/@nhtio/adk/common/functions/runOnion.md'
---

# Function: runOnion()

```ts
function runOnion<Ctx, Res>(
   ctx: Ctx,
   use: readonly (ctx: Ctx, next: NextFn) => void | Promise<void>[],
   core: () => Promise<Res>,
onNoNext: () => never): Promise<Res>;
```

Defined in: [src/lib/middleware/run\_onion.ts:12](https://github.com/NHTIO/ADK/blob/v1.20261103.0/src/src/lib/middleware/run_onion.ts#L12)

Execute an asynchronous middleware onion around a terminal operation.

A fresh runner is deliberately created for each invocation: middleware runners are single-use.
Errors are captured and re-thrown unchanged — including a thrown `undefined`, which a
value-based sentinel would silently swallow — while a chain that stops without invoking its
terminal operation is reported through `onNoNext`.

## Type Parameters

| Type Parameter |
| -------------- |
| `Ctx`          |
| `Res`          |

## Parameters

| Parameter  | Type                                                                         |
| ---------- | ---------------------------------------------------------------------------- |
| `ctx`      | `Ctx`                                                                        |
| `use`      | readonly (`ctx`: `Ctx`, `next`: `NextFn`) => `void` | `Promise`<`void`>\[] |
| `core`     | () => `Promise`<`Res`>                                                     |
| `onNoNext` | () => `never`                                                                |

## Returns

`Promise`<`Res`>
