Skip to main content
Module

x/async_channels/mod.ts>time.Timer#reset

Inspired by Go & Clojure Channels, async_channels provides channels as an asynchronous communication method between asynchronous functions.
Latest
method time.Timer.prototype.reset
import { time } from "https://deno.land/x/async_channels@v1.0.0-rc8/mod.ts";
const { Timer } = time;

reset changes the timer to expire after duration. It returns true if the timer had been active, false if the timer had expired or been stopped.

reset should be invoked only on stopped or expired timers with drained channels.

If a program has already received a value from t.c, the timer is known to have expired and the channel drained, so t.reset() can be used directly. If a program has not yet received a value from t.c, however, the timer must be stopped and—if stop reports that the timer expired before being stopped—the channel explicitly drained:

if (!t.stop()) {
  await t.c.get()
}
t.reset(d)

This should not be done concurrent to other receives from the Timer's channel.

Note that it is not possible to use reset's return value correctly, as there is a race condition between draining the channel and the new timer expiring. reset should always be invoked on stopped or expired channels, as described above. The return value exists to preserve compatibility with existing programs.

Parameters

duration: number

A safe and non-negative integer.

Returns

boolean