[][src]Function smol::block_on

pub fn block_on<T>(future: impl Future<Output = T>) -> T

Blocks on a single future.

This function polls the future in a loop, parking the current thread after each step to wait until its waker is woken.

Unlike run(), it does not run executors or poll the reactor!

You can think of it as the easiest and most efficient way of turning an async operation into a blocking operation.

Examples

use futures::future;
use smol::{Async, Timer};
use std::thread;
use std::time::Duration;

// Run executors and the reactor on a separeate thread, forever.
thread::spawn(|| smol::run(future::pending::<()>()));

smol::block_on(async {
    // Sleep for a second.
    // This timer only works because there's a thread calling `run()`.
    Timer::after(Duration::from_secs(1)).await;
})