3 ms·
> I've also read that await() basically preempts the currently running function. How does this work? Well, I would say that it's the opposite of preemption. It
by klodolph 5y ago
> I've also read that await() basically preempts the currently running function. How does this work?
Well, I would say that it's the opposite of preemption. It's cooperative multitasking. Your async task is split into two tasks, with the await in the middle. When you call await, the first task finishes. The second task starts running once the await is done.
> Why can't non-async functions use await()?
In C#, they can. You can call `.RunSynchronously()` on a `Task`. C# supports lots of different TaskSchedulers that handle this differently.
C# is almost pathologically flexible here. Other languages typically assume that there is only one async task scheduler, and it's handled by the runtime. This task scheduler may be less flexible, and there are various design tradeoffs.