Reading through the “Resolve
await syntax” thread and pondering various syntax options, I have begun to wonder what the difference between
yield really is. AFAICT (and please correct me where I’m wrong!):
- in a generator,
yieldsaves the state of the closure and returns the yielded value, suspending the closure until it is resumed.
- in an
awaitsimilarly yields a future, but to a conceptual second, outer closure that handles polling the yielded future (i.e., the future being awaited) before the inner closure can be resumed.
However, it is the function being declared
async that creates that second, interposing closure. But from the point of view of the closure created by the user code, it does the same as in a run-of-the-mill generator: yielding the value, which just so happens to be a
Future, and waiting to be resumed.
IOW, is there an actual difference between
yield, or couldn’t we just use the same keyword/operator/whatever the final syntax will be? In a regular closure it’d just yield the future, in an
async closure that yielded future and resumption both being intercepted by the interposing code created by