Coroutine
A coroutine is a function that can be suspended and resumed, switching cooperatively without OS scheduling, lighter than a thread.
Purpose
Write asynchronous code with synchronous-style syntax, more intuitive than callbacks/Promises.
Mechanism
A coroutine suspends (yield/await), returning control to the caller, and resumes from the suspension point. With no OS thread-switching overhead, a single thread can run millions of coroutines. Python async/await, Go goroutine, JS async/await.
Use cases
- Asynchronous programming
- High concurrency
- Lightweight scheduling