Inherited from v1.0.0

timers module

Two functions, both drive callbacks from the event loop. There is no setTimeout / setImmediate / setInterval — use these.

import "timers" as timers;

timers.set(1000, function () { print("one second later"); });

var cancel = timers.interval(500, function () { print("every half second"); });
// later:  cancel();

API

FunctionDescription
timers.set(delay_ms, fn)Run fn once, delay_ms from now. Pass 0 to defer to the next tick. Returns a cancel fn.
timers.delay(delay_ms) → FutureFuture-returning sleep. wait timers.delay(100); suspends an async function without writing a callback.
timers.interval(delay_ms, fn) → cancel-fnRun fn every delay_ms. Returns a function — call it to stop.
function paced() {
    print("a");
    wait timers.delay(100);
    print("b after 100ms");
    wait timers.delay(100);
    print("c after another 100ms");
}

Notes

  • timers.set(0, fn) is the bnl equivalent of Node's setImmediate and process.nextTick.
  • timers.delay(0) is the Future-returning equivalent — wait timers.delay(0) yields to the loop once.
  • Hold on to the cancel handle returned by interval — otherwise the timer keeps your process alive forever.
  • Callbacks share the event loop with every other in-flight task. Keep them short.

See also