Async and await keywords
📖 Scenario: You are building a simple console app that fetches data from a server. To keep the app responsive, you will use async and await keywords to handle the data fetching without blocking the main thread.
🎯 Goal: Create an asynchronous method to simulate fetching data, then call it using await and display the result.
📋 What You'll Learn
Create an asynchronous method called
FetchDataAsync that returns a Task<string>.Inside
FetchDataAsync, simulate a delay of 2 seconds using Task.Delay.Return a string message from
FetchDataAsync after the delay.In the
Main method, call FetchDataAsync using await and store the result in a variable called data.Print the
data variable to the console.💡 Why This Matters
🌍 Real World
Many apps need to fetch data from the internet or perform long tasks without freezing the screen. Using <code>async</code> and <code>await</code> helps keep apps smooth and responsive.
💼 Career
Understanding asynchronous programming is essential for software developers working on web apps, desktop apps, or any program that handles input/output or network calls efficiently.
Progress0 / 4 steps