Skip to content

Instantly share code, notes, and snippets.

@oofnivek
Last active June 6, 2025 00:06
Show Gist options
  • Save oofnivek/e325fba831b31b339e62952fed8f71bd to your computer and use it in GitHub Desktop.
Save oofnivek/e325fba831b31b339e62952fed8f71bd to your computer and use it in GitHub Desktop.
Async wrongly
namespace LearnAsync;
class Program
{
static async Task Main(string[] args)
{
var start = DateTime.Now;
Coffee coffee = PourCoffee();
Console.WriteLine("coffee is ready");
Egg egg = await FryEggAsync(2); // async function
Console.WriteLine("eggs are ready");
Bacon bacon = await FryBaconAsync(3); // async function
Console.WriteLine("bacon is ready");
Toast toast = await ToastBreadAsync(2); // 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