Open irh opened 8 months ago
Some groundwork for this was implemented in #291 - the VM now has an execution_state
field which has a Suspended
variant, used when a generator has just yielded a value.
Additionally, continue_running
now returns a ReturnOrYield
enum that lets the caller know the nature of the resulting value.
ReturnOrYield
could be extended (and renamed) to include an Await
variant that passes the task that's being waited on to an executor (or something 🙃).
It would be good to have async functions in Koto, and I think that it would be a natural extension of the existing Generator concept to add support for async workflows.
Generator functions work by creating an iterator when first called, which contains a VM that's responsible for executing the function, suspending execution each time
yield
is encountered. Each time.next()
is called the iterator runs until the nextyield
expression, or the function exits.I think generators could be replaced with a new
Task
value type that supports anawait
expression alongside the existingyield
.await
would suspend execution of the task while waiting for output from some other task. The task's VM would either block on the awaited result becoming available, or it could be managed by an async runtime that monitors the progress of multiple parallel tasks.So, the new
Task
type will be able to:await
).