Skip to content

sleep ​

sleep blocks the current thread for the requested number of milliseconds.

Signaturesleep(ms: u64): void

Use sleep in command-line programs, simple scripts, and tests where blocking the current thread is intended. In an async program with one worker thread, it blocks the entire runtime.

What continues while waiting ​

Code below an await sleepAsync(...) in the same function waits for the delay. Other ready tasks may run on that thread while the current function is suspended. In the first program, sleep blocks the thread, so it prints first and then second. In the second program, sleepAsync suspends the first task, so it prints second and then first.

tyrus
import { sleep } from "tyrus:task";

function main(): void {
    sleep(1000);
    print("first"); // Prints first.
    print("second"); // Prints second.
}
tyrus
import { sleepAsync, spawn } from "tyrus:task";

async function first(): Promise<void> {
    await sleepAsync(1000);
    print("first"); // Prints second, after sleepAsync completes.
}

async function second(): Promise<void> {
    print("second"); // Prints first, while the first task waits.
}

async function main(): Promise<void> {
    const firstTask = spawn(first);
    const secondTask = spawn(second);
    await firstTask.join();
    await secondTask.join();
}

error

tyrus
import { sleep } from "tyrus:task";

function main(): void {
    sleep(-1); 
}
text
error[E0308]: type mismatch for `argument 1 of `sleep``: expected `u64`, found `negative integer literal`
at /src/main.tyr:4:5
    sleep(-1);
    ^^^^^
help: change the value or the annotation so their types match; convert numbers explicitly with `value as <type>`
error[E0706]: integer literal `-1` does not fit in `u64`, which holds values from 0 to 18446744073709551615
at /src/main.tyr:4:11
    sleep(-1);
          ^^
help: the narrowest type that holds this value is `i8`; widen the type or change the value to fit