C#/Unity asynchronous processing for async/await

Asynchronous processing can generally be implemented using call back to to bring up the method after completion. It is often used in Unity as well, but

Complex processing creates multiple nests

During this, internal exclusions are not transmitted externally

You can no longer see the processing order from the code

Such problems result in what is known as call back hell.

In place of this, Unity provides asynchronous processing through coroutines done with yield return (generator).

This coroutine asynchronous processing on Unity has the benefit of being able to be written sequentially, but at the same time

It combines with MonoBehaviour during startup

Transmits the return value

Transmits exceptions

Controls multiple coroutines (serial/parallel processing)

To mutually supplement the above deficiencies, write the long complex processes with coroutines, transmit the values through call backs, and try to prevent the nests from becoming deep as much as possible. This is the way we have dealt with this until now.

You can incorporate UniRx to lessen the complexity of this kind of asynchronous processing, but

Its functions are too powerful, which tends to create puzzle-like complexity

You cannot tell the difference between events (∞ length) and asynchronity (1 in length) with just `IObervable<T>`

You cannot write it procedurally, making descriptions less easy to understand

These side effects prevent it from becoming to silver bullet.

async/await is designed to be able to “write asynchronous processing like synchronous processing” as a function of the language, which makes it possible to write matrices like the following to distinguish its usage from Rx.

Processing the single return values of synchronization is done by simply calling up the method, but the single return values of asynchronization only requires you to write await there. And like you would process multiple return values for synchronization using foreach, doing event processing in Rx allows you to distinguish naturally between uses of the two.

AsyncEnumerable incorporated through > C# 8.0 makes asynchronous foreach possible. It is different from Rx in that AsyncEnumerable can do Pull asynchronous sequence processing, and IObservable can do Push asynchronous sequence processing.

By using async/await

You can write procedurally and nests disappear

You can naturally handle return values, exception processing, and synchronous processing

You can now make descriptions in an ideal manner. However, no processing can exist without side effects, and async/await has the following drawbacks