How does async differ from a Rc<dyn Fn () -> T>?

Regular function keeps running continuously until it returns, and this means it needs to have a thread and a stack, for the entire duration of the function call, even while it calls other functions or waits for something.

async functions can be "paused", and while they're paused they don't use any threads or stack. Behind the scenes they're not really functions that you call, but objects that can be asked many times "are you finished yet?". For many many tasks, especially network-bound, it's easier to deal with objects that represent them than with threads and functions waiting on them.

It's sort of like the difference between for {} loop that runs until completion vs Iterator that you can call next() on whenever you want.

3 Likes