Build fluency in the vocabulary of a function that suspends and later resumes with all of its local state intact.
0 / 5 completed
1 / 5
At standup, a dev mentions a function that can suspend its own execution partway through, yielding control back to its caller, and later resume exactly where it left off with all of its local state intact. What is this construct called?
A coroutine is exactly this: a function that can suspend its own execution partway through, yielding control back to whatever resumed it, and later pick up exactly where it left off with all of its local variables and execution position preserved, rather than starting over from the beginning. A hash collision is an unrelated hash-table concept about two keys sharing a bucket. This suspend-and-resume-with-preserved-state behavior is exactly why coroutines are the foundation for writing asynchronous code that reads like straightforward, sequential logic.
2 / 5
During a design review, the team writes an asynchronous data-fetching sequence as a coroutine that suspends while waiting on a network call and resumes automatically once the response arrives. Which capability does this provide?
Writing the sequence as a coroutine provides sequential-looking code that handles asynchronous waiting without blocking the underlying thread, since suspending at the network call frees that thread to do other useful work until the response arrives, at which point the coroutine resumes automatically right where it left off. Blocking the thread entirely while waiting would tie up that thread's resources for the whole duration of the network call, unable to do anything else in the meantime. This non-blocking suspension is exactly why coroutines let asynchronous logic read like simple, top-to-bottom sequential code while still avoiding wasted thread time.
3 / 5
In a code review, a dev notices an asynchronous data-fetching flow is implemented as a deeply nested chain of callbacks, one triggered after another, instead of a coroutine that could express the same sequence as straightforward, sequential-looking code. What does this represent?
This is a missed coroutine opportunity, since a deeply nested callback chain forces the asynchronous sequence's logic to be scattered across many separate callback functions, when a coroutine would let that exact same sequence be written as straightforward, sequential-looking code that simply suspends at each asynchronous step and resumes once it completes. A cache eviction policy is an unrelated concept about discarded cache entries. This nested-callback pattern is exactly the kind of hard-to-follow structure a coroutine is designed to replace with clearer, linear-looking logic.
4 / 5
An incident report shows a maintenance change to an asynchronous data-fetching flow introduced a subtle bug, because the logic was spread across a deeply nested chain of callbacks that made the actual order of operations hard to follow. What practice would prevent this?
Rewriting the flow as a coroutine expresses the same asynchronous sequence as sequential-looking code, with suspension points instead of nested callback functions, which makes the actual order of operations far easier to follow and directly addresses the maintainability problem behind this incident. Continuing to implement the flow as a deeply nested callback chain regardless of how hard it is to follow is exactly what let the subtle bug slip through during maintenance. This coroutine-based rewrite is the standard fix for asynchronous logic that has grown difficult to reason about as a tangle of nested callbacks.
5 / 5
During a PR review, a teammate asks why the team reaches for coroutines instead of just spawning a dedicated operating-system thread for every concurrent asynchronous task. What is the reasoning?
A coroutine is far lighter-weight than an operating-system thread, since suspending and resuming it doesn't require the operating system's full thread-scheduling machinery, letting many thousands of coroutines run concurrently on top of a comparatively small pool of actual threads. The tradeoff is that coroutines depend on language or runtime support for suspending and resuming them, and mixing coroutine-style code with traditional blocking code requires care. This lightweight-concurrency advantage is exactly why coroutines are favored for workloads with a huge number of concurrent, mostly-waiting tasks, like network requests, over spawning a dedicated thread for each one.