Skip to content

sleepAsync ​

sleepAsync resolve após a quantidade solicitada de milissegundos sem bloquear uma thread do executor.

AssinaturasleepAsync(ms: u64): Promise<void>

Aguarde a Promise retornada dentro de uma função async. O código abaixo desse await na mesma função espera o atraso, enquanto outras tasks prontas podem continuar na thread do executor.

Para uma espera bloqueante em código síncrono, use sleep.

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

async function main(): Promise<void> {
    print("before delay");
    await sleepAsync(25);
    print("after delay");
}

error

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

function main(): void {
    await sleepAsync(25); 
}
text
error[async-await-outside-async]: `await` can only be used inside an async function
at /src/main.tyr:4:5
    await sleepAsync(25);
    ^^^^^
help: move this `await` into an `async` function or mark its enclosing function `async`