# Server.using(_:_:)

- **Module:** Server
- **Package:** libtmux-swift
- **Language:** Swift
- **Kind:** method
- **Source:** https://github.com/libtmux/libtmux-swift/blob/f02a4668570e1cc5198c941413750e021f42c214/Sources/LibTmux/ControlMode.swift#L131
- **Page:** https://libtmux.org/reference/swift/server-using(_-_-)/

```
Server.using(_:_:)(mode: TmuxMode, body: @escaping (Server) async throws -> Result) -> Result
```

Runs `body` with this server in `mode`.

The one switch. Every call inside is the call you would write anyway and
hands back the type it would hand back anyway; `mode` decides only how it
travels. Reach for this when the choice is made at runtime — a flag, a
config, a benchmark running both — so it stays a value rather than two
shapes of code:

```swift
let mode: TmuxMode = attachToExisting ? .connected(to: "main") : .direct
let names = try await server.using(mode) { server in
    try await server.sessions().map(\.name)
}
```

Scoped even for ``TmuxMode/direct``, where nothing needs closing, so that
the two read identically at the call site. ``connected(attachingTo:_:)``
is the same thing with the connection handed over as well, for the one
capability a process does not have.

Modes nest, and the innermost wins: `using(.direct)` inside a connected
scope gives back a server that spawns processes, which is the supported
way to keep one call off a connection.
A server handed to a connected `body` does not keep that connection
alive when returned or stored.

## Raises

- `Result`
