Skip to content

Sleep

On this page

sleep suspends the current coroutine for the given duration. During the pause, the scheduler can run other tasks — it is cooperative, not blocking.

There are two equivalent forms: the keyword form (sleep 50ms) and the function form (sleep(pause)). Use the function form when the duration comes from a variable.

Keyword form and function form; sleep 0ms yields control for one tick without waiting.

04-sleep.zolo
Playground
// Feature: sleep for a duration

// Syntax: two forms — keyword `sleep <dur>` and function `sleep(<dur>)`

// When to use: pause execution of the current coroutine. Cooperative:

// other scheduler tasks may run while you sleep.


print("before")

// Keyword form — direct, no parentheses.

sleep 50ms

print("middle")

// Function form — useful when the duration comes from a variable.

let pause = 30ms
sleep(pause)

print("after")

// expected:

// before

// middle

// after


// Yield to the scheduler with `sleep 0s` — hands over control for one tick.

sleep 0ms
print("one tick later")

// expected: one tick later


// Function form again — useful from a builtin context.

print("before final sleep")
sleep(20ms)
print("after final sleep")
// expected:

// before final sleep

// after final sleep

sleep 0ms is useful for yielding the scheduler — it ensures other coroutines get a chance to run without introducing any real delay.

Search Zolo

9 results

enespt-br