# server.Server.withConnection

- **Module:** server
- **Package:** libtmux
- **Language:** TypeScript
- **Kind:** method
- **Source:** https://github.com/libtmux/libtmux-ts/blob/073b9d7dedad900bff6f5ec0ea47794e40225870/packages/libtmux/src/server.ts#L529
- **Page:** https://libtmux.org/en/ts/latest/reference/server-server-withconnection/

```
async withConnection<T>(
  body: (live: ConnectedServer) => Promise<T>,
  options?: ConnectOptions,
): Promise<T>
```

Run `body` against a connected server, closing it afterwards.

The scoped form of [`connect`](/en/ts/latest/reference/server-server-connect/), for code that cannot use `await using`
— it needs `Symbol.asyncDispose` in the consumer's `lib` — or would rather
not manage the lifetime by hand. The connection closes on the way out
whether `body` returns or throws.

## Examples

```ts
const opened = await server.withConnection(async (live) => {
  await session.newWindow({ name: "build" });
  return live.waitFor((current) => current.windows.exists({ name: "build" }));
});
opened.windows.count({ name: "build" }); // 1
```

## Type parameters

- `T`

## Parameters

- `body` (`(live: ConnectedServer) => Promise<T>`)
- `options?` (`ConnectOptions`)
