Skip to content

sleep ​

sleep bloquea el hilo actual durante la cantidad solicitada de milisegundos.

Firmasleep(ms: u64): void

Use sleep en programas de línea de comandos, scripts simples y pruebas donde bloquear el hilo actual sea intencional. En un programa asíncrono con un único hilo de trabajo, bloquea todo el runtime.

Qué continúa durante la espera ​

El código debajo de await sleepAsync(...) en la misma función espera a que termine el retraso. Otras tasks listas pueden usar el hilo mientras la función actual está suspendida. En el primer programa, sleep bloquea el hilo, por lo que imprime first y después second. En el segundo programa, sleepAsync suspende la primera task, por lo que imprime second y después 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