Last active
June 6, 2025 00:13
-
-
Save oofnivek/cca29e5b00653fec22697a8ab0f1dd76 to your computer and use it in GitHub Desktop.
Async correctly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace LearnAsync; | |
| class Program | |
| { | |
| static async Task Main(string[] args) | |
| { | |
| var start = DateTime.Now; | |
| var taskEgg = FryEggAsync(2); // declaring task at the top | |
| var taskBacon = FryBaconAsync(3); // declaring task at the top | |
| var taskToast = ToastBreadAsync(2); // declaring task at the top | |
| Coffee coffee = PourCoffee(); | |
| Console.WriteLine("coffee is ready"); | |
| Egg egg = await taskEgg; // await the task not the async function | |
| Console.WriteLine("eggs are ready"); | |
| Bacon bacon = await taskBacon; // await the task not the async function | |
| Console.WriteLine("bacon is ready"); | |
| Toast toast = await taskToast; // await the task not the async function | |
| ApplyButter(toast); | |
| ApplyJam(toast); | |
| Console.WriteLine("toast is ready"); | |
| Juice juice = PourJuice(); | |
| Console.WriteLine("breakfast is ready"); | |
| var end = DateTime.Now; | |
| Console.WriteLine("total time= {0} pretend minutes", (end - start).Seconds); | |
| } | |
| static Juice PourJuice() | |
| { | |
| Console.WriteLine("pouring juice"); | |
| return new Juice(); | |
| } | |
| static Coffee PourCoffee() | |
| { | |
| Console.WriteLine("pouring coffee"); | |
| return new Coffee(); | |
| } | |
| static void ApplyJam(Toast toast) | |
| { | |
| Console.WriteLine("putting jam on the toast"); | |
| } | |
| static void ApplyButter(Toast toast) | |
| { | |
| Console.WriteLine("putting butter on the toast"); | |
| } | |
| async static Task<Toast> ToastBreadAsync(int quantity) | |
| { | |
| for (int i = 0; i < quantity; i++) | |
| { | |
| Console.WriteLine("putting a slice of bread in the toaster"); | |
| } | |
| Console.WriteLine("start toasting"); | |
| for (int i = 0; i < 3; i++) | |
| { | |
| await Task.Delay(1000); | |
| Console.WriteLine($"toasting {i}"); | |
| } | |
| Console.WriteLine("remove toast from toaster"); | |
| return new Toast(); | |
| } | |
| async static Task<Bacon> FryBaconAsync(int quantity) | |
| { | |
| Console.WriteLine($"putting {quantity} slices of bacon in the pan"); | |
| Console.WriteLine("cooking first side of bacon"); | |
| for (int i = 0; i < 3; i++) | |
| { | |
| await Task.Delay(1000); | |
| Console.WriteLine($"frying bacon {i}"); | |
| } | |
| for (int i = 0; i < quantity; i++) | |
| { | |
| Console.WriteLine("flipping slice of bacon"); | |
| } | |
| Console.WriteLine("cooking the other side of bacon"); | |
| for (int i = 0; i < 3; i++) | |
| { | |
| await Task.Delay(1000); | |
| Console.WriteLine($"frying bacon {i}"); | |
| } | |
| Console.WriteLine("putting bacon on plate"); | |
| return new Bacon(); | |
| } | |
| async static Task<Egg> FryEggAsync(int quantity) | |
| { | |
| Console.WriteLine($"cracking {quantity} eggs"); | |
| Console.WriteLine("cooking the eggs"); | |
| for (int i = 0; i < 6; i++) | |
| { | |
| await Task.Delay(1000); | |
| Console.WriteLine($"frying eggs {i}"); | |
| } | |
| Console.WriteLine("put eggs on plate"); | |
| return new Egg(); | |
| } | |
| } | |
| class Juice() { } | |
| class Coffee() { } | |
| class Toast() { } | |
| class Bacon() { } | |
| class Egg() { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment