34

Given an async function and it's corresponding future, lets say:

async fn foo() -> Result<i32, &'static str> {
    // ...
}

let my_future = foo();

what is the difference between awaiting it using just .await other than using tokio::spawn().await?

// like this...
let result1 = my_future.await;

// ... and like this
let result2 = tokio::spawn(my_future).await;
hbobenicio
  • 1,000
  • 15
  • 16
  • 1
    `my_future.await` will need to complete before any code that appears immediately after will run. Spawning a new task will continue with the current task, while running the future in a new task. – Peter Hall Jun 26 '20 at 13:33

1 Answers1

40

One typically doesn't await a spawned task (or at least not right away). It's more common to simply write:

tokio::spawn(my_future);

Leave out the .await and the task will run in the background while the current task continues. Immediately calling .await blocks the current task. spawn(task).await is effectively no different than task.await. It's akin to creating a thread and immediately joining it, which is equally pointless.

Spawned tasks don't need to be awaited the way bare futures do. Awaiting them is optional. When might one want to await one, then? If you want to block the current task until the spawned task finishes.

let task = tokio::spawn(my_future);

// Do something else.
do_other_work();

// Now wait for the task to complete, if it hasn't already.
task.await;

Or if you need the result, but need to do work in between starting the task and collecting the result.

let task = tokio::spawn(my_future);

// Do something else.
do_other_work();

// Get the result.
let result = task.await;
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    I think I've got it. I thought both futures (my_future and the future returned by tokio::spawn) would do nothing until awaited. I thought every future were lazy. I think I was wrong then and your explanation makes a lot of sense! Thank you. – hbobenicio Jun 26 '20 at 13:55
  • 4
    @hbobenicio I have the same question too today. Looks like `tokio::spawn` like `thread::spawn`, when you spawn, it runs background. – ccQpein Aug 01 '20 at 20:35