Skip to content

Coroutine

Spicy — senior dev territoryGeneral Dev

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.