Language guide · 10 of 12
Concurrency: go, await, & Task
Evie provides built-in cooperative concurrency inspired by Go goroutines and JavaScript async/await.
Spawning Tasks (go)
Use the go keyword followed by a function call to run it concurrently:
task := go fetchRemoteData()
// Or invoke an inline anonymous function:
inline := go fn() {
return fetchRemoteData()
}()Open in playgroundAwaiting Tasks (await)
await pauses the current fiber until the task completes and unwraps the result:
data := await taskOpen in playground- Multi-await memoization: Awaiting the same task multiple times immediately returns the memoized result without re-executing.
- Identity await: Awaiting a non-task value (e.g.
await 42) is a safe no-op that returns the value.
Multi-Task Concurrency (await.all, await.any, await.race, await.timeout)
t1 := go fetchServiceA()
t2 := go fetchServiceB()
// Wait for all to succeed (returns array of results)
results := await.all(t1, t2)
// Or pass an array:
results = await.all([t1, t2])
// Wait for first successful task (ignores failures unless all fail)
firstSuccess := await.any(t1, t2)
// Wait for first task to settle (whether success or error)
winner := await.race(t1, t2)
// Wait for a task with a deadline in milliseconds (cancels and throws on timeout)
bounded := await.timeout(t1, 500)Open in playgroundTask Methods
t.done() // returns boolean: whether the task has settled
t.isDone() // alias for t.done()
t.result() // returns task result (or nil)
t.error() // returns error (or nil)
t.await() // method form of await
t.cancel() // requests cooperative cancellation of the taskOpen in playgroundShared State
Only one fiber of an instance executes Evie code at a time. The rule for when it hands over is short: a fiber yields only when it waits. That happens in exactly three places:
- it awaits a task, in any form (
await t,await.all,await.any,await.race,await.timeout,t.await()); - it finishes, by returning or throwing;
- it calls a function that waits on the outside world:
time.wait,io.prompt,io.readlnand thefsfunctions that touch the disk. The Standard Library tables mark these yields; no other standard function yields. A Go host can add its own (seeBlockingunder Go Host Embedding).
Nothing else hands over: arithmetic, assignments, method calls, go itself, io.println, json, math. A call to one of your own functions yields only if it reaches one of the three places, as a helper that calls time.wait does. Between those points a fiber runs uninterrupted, so shared variables, arrays, objects and classes never need locks:
var counter := 0
fn worker(n) {
while n > 0 {
counter += 1 // never lost: no other fiber runs until this one awaits
n -= 1
}
}
fn main() {
await.all(go worker(500), go worker(500))
return counter // always 1000
}Open in playground- No data races: a script cannot corrupt memory or crash the host by sharing values between tasks.
- Cooperative handover: a task spawned with
gostarts running when the current fiber yields: it awaits, finishes, or calls a function that waits. - Parallelism across instances: to use several cores, a host runs several
vm.Instances. Each has its own heap; nothing is shared between them.