How to Await Anything in C#

Master await in C# with practical use-cases

Arnold Abraham
CodeX

--

Image made By the Author via Canva

Did you know that you can await anything in C#? Seems irresponsible, but I want to answer the question if there Is a need for such a thing?

Recently I saw a YouTube Video from Nick Chapsas and wanted to try out what he explained. While his advice was to use this feature better not, you will understand the topic of awaitables better.

Last but not least, I can justify some real-world use cases.

Why Await Anything & the Practical Use Cases

It seems obvious not to await anything.

But if you are developing, for example, with Unity, you can create custom awaiters. Unity has its own concurrency solution called Coroutines, but now they also support async/await in C#.

Hence, custom awaiters allow you to await a Coroutine from a Task, which brings you sophisticated and easily maintainable code.

You can also create custom awaitables and awaiters to establish micro-threading. For example, one that does not restore synchronization context.

Here is an example that is impossible to replicate using a taskAwaiter, but it’s possible with a custom waiter:

await new SwitchToRandonThread();

--

--

Arnold Abraham
CodeX
Writer for

JavaScript, TypeScript and C#/.NET Tutorials/News/Best Practices by a German Software Engineer - Fun helps you to learn on the fly --> arnoldcode.com