# blocking.Runtime.try_run

- **Module:** blocking.Runtime
- **Package:** libtmux
- **Language:** Rust
- **Kind:** method
- **Source:** https://github.com/libtmux/libtmux-rs/blob/8a648c0894dfffc9303583f753b6c8ae01f2fe76/crates/libtmux/src/blocking.rs#L116
- **Page:** https://libtmux.org/reference/rs/blocking-runtime-try_run/

```
blocking.Runtime.try_run(self, future: F) -> Result<F::Output, Error>
```

Run one future to completion, or say why it cannot be run here.

Same as [`run`], except that being inside an async context is returned
rather than raised. This exists because the callers this module is for
are the ones most likely to hit it: a script that grows a `#[tokio::main]`,
or a helper written for a script and later called from an async test.

[`run`]: Self::run

# Errors

Returns [`Error::RuntimeNested`] when called from inside an async
context. Await the future directly there.

## Example

Outside an async context it runs the future and gives back its value:

```rust
use libtmux::blocking::Runtime;

let runtime = Runtime::new()?;
let answer = runtime.try_run(async { 2 + 2 })?;
assert_eq!(answer, 4);
```

## Example

Inside one it says so, where [`run`] would panic:

```rust
use libtmux::{Error, ErrorKind};
use libtmux::blocking::Runtime;

let runtime = Runtime::new()?;
let nested = runtime.run(async { runtime.try_run(async { 2 + 2 }) });

assert!(matches!(nested, Err(Error::RuntimeNested)));
assert_eq!(nested.unwrap_err().kind(), ErrorKind::InvalidInput);
```
