Coroutines are a major paradigm in concurrent and asynchronous programming, mainly focused around pausing execution, and yielding control while waiting for something to happen.
Most languages distil coroutines into two major keywords: async and await.
asyncis used to tag a function as asynchronous. This allows it to suspend execution.awaitis used to suspend execution and yield control back to an event loop while it waits for a value (usually a function call or IO). It allows the next (non awaiting) piece of code to take control and execute.yieldallows it to unconditionally yield control back to an event loop.
Coroutines are the major mechanism that asynchronous programming is done in most modern languages, including in Kotlin, Python, C++20, and Go. They’re more preferable to threads because they’re less complicated to synchronise, and to events, because they build on top of them.