Coroutine
ELI5 — The Vibe Check
A coroutine is a function that can pause itself, let other things run, then resume right where it left off. It's like bookmarking your place in a book, reading another book for a while, then coming back. They make async code read like normal code.
Real Talk
A programming construct that generalizes subroutines for non-preemptive multitasking by allowing execution to be suspended and resumed at specific points. Coroutines enable cooperative concurrency, async/await patterns, generators, and efficient I/O-bound operations without OS threads. Used in Kotlin, Python (asyncio), Go (goroutines), and C++20.
Show Me The Code
# Python coroutine with async/await
import asyncio
async def fetch_data(url):
await asyncio.sleep(1) # suspend here
return f"Data from {url}" # resume here
async def main():
results = await asyncio.gather(
fetch_data("api/a"),
fetch_data("api/b"),
)
print(results)
When You'll Hear This
"Kotlin coroutines make Android async code so much cleaner than callbacks." / "Coroutines are lighter than threads — you can run millions of them without breaking a sweat."
Related Terms
Async/Await
Async/await is syntactic sugar that makes Promises look like normal, readable code. Instead of chaining .then().then().
Concurrency
Concurrency is juggling multiple tasks at once — not necessarily at the exact same instant, but switching between them fast enough that they all seem to be...
Green Thread
Green threads are fake threads managed by your programming language instead of the operating system.