Pular para o conteúdo

Sleep

Nesta página

sleep suspende a corrotina atual pelo tempo indicado. Durante a pausa, o scheduler pode executar outras tarefas — é cooperativo, não bloqueante.

Existem duas formas equivalentes: a forma keyword (sleep 50ms) e a forma função (sleep(pausa)). Use a forma função quando a duração vem de uma variável.

Forma keyword e forma função; sleep 0ms cede controle por um tick sem esperar.

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 é útil para ceder o scheduler — garante que outras corrotinas tenham chance de rodar sem introduzir atraso real.

Buscar no Zolo

9 resultados

enespt-br