# server.Server.withConnection

- **Module:** server.Server
- **Package:** @libtmux/libtmux
- **Language:** TypeScript
- **Kind:** method
- **Source:** https://github.com/libtmux/libtmux-ts/blob/2cb93308200da38a26b59876618116850f110607/packages/libtmux/src/server.ts#L219
- **Page:** https://libtmux.org/reference/ts/server-server-withconnection/

```
server.Server.withConnection(body: : (live: ConnectedServer) => Promise<T>, options: : ConnectOptions) -> Promise<T>
```

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

The scoped form of {@link 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.

## Example

```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
```
