execute_async

Function execute_async 

Source
pub fn execute_async<F, T>(future: F) -> Result<T, String>
where F: Future<Output = Result<T, String>> + Send + 'static, T: Send + 'static,
Expand description

Execute an async function in a sync context

Detects if we’re already in a tokio runtime and uses Handle::current() if so, otherwise creates a new runtime. This prevents nested runtime panics.

§Examples

use cli::runtime_helper::execute_async;

fn sync_function() -> Result<String, String> {
    execute_async(async {
        let data = fetch_data().await?;
        Ok(data)
    })
}

§Errors

Returns error if:

  • Runtime creation fails
  • The async future returns an error