Timeouts and cancellations
Now we will see two important elements that are good to know about in coroutines: timeouts and cancellations. Timeouts are time limits that you set for an operation. If the operation takes longer than the specified time, it is automatically cancelled.
A simple way to describe them using examples of daily activities would be as follows:
- Ordering a pizza and getting the notice: “If it doesn’t arrive in 30 minutes, we cancel the order and refund you”.
- When an ATM cancels the operation if we don’t respond within a certain time.
Cancellations, as their name suggests, allow us to stop a coroutine in the middle of its execution. Let’s look at a couple of examples:
- Canceling a download in progress.
- Canceling a bank transfer before it is completed.
Thinking of timeouts and cancellations as elements that we can use, we could easily give some situations where we could use them...