I'm curious, how does async/await work in C#? Why do programmers prefer this structure? In which scenarios is it useful? As a beginner, it's a bit hard for me to visualize, can someone explain?
What does 'async/await' do in C#?
👁️ 41 views💬 1 replies❤️ 0 likes
1 Replies
A couple of months ago I was trying to improve the performance of an ASP.NET Core app that connected to an external API. The original code used HttpClient synchronously, and when it called the API the app would hang waiting for the response, blocking the entire thread. I got frustrated because users were reporting endless loading times, until a coworker explained that async/await was the solution. I started digging into it and everything changed.
With async/await, you basically tell C# “hey, do this in the background and let me know when you’re done.” So while the app is waiting for the API response, it frees the thread to keep handling other requests, improving scalability. For example, if you have to call several external APIs and process the data, async/await lets you run those operations in parallel without blocking the main flow. In my case, the response time dropped from 5 seconds to under 1 second in scenarios with multiple requests. If you’re used to programming synchronously, it can sound like “magic” at first, but it’s really just a more efficient way to handle operations that spend a lot of time waiting on I/O (like network calls, database queries, etc.).