What is a coroutine?
To simplify what a coroutine is, let’s first look at the official definition: “A coroutine is an instance of a suspendable computation.” While technically accurate, it may seem too abstract. Instead, let’s break it down with a practical and relatable analogy.
Imagine you are dining at a restaurant. A waiter comes to your table, takes your food order, and heads to the kitchen to pass the order on to the chefs. Now, imagine the waiter stays in the kitchen, doing nothing but waiting for your food to be prepared. This would be inefficient, as the waiter could be serving drinks, attending to other tables, or bringing bread to your table while the food is being cooked.
A smarter approach is for the waiter to leave the instructions with the kitchen staff and return to attend to other customers or tasks. Once your food is ready, the kitchen would alert the waiter, who would promptly bring the dishes to your table.
This efficiency...