strict-timeout v1.1.0
strict-timeout
setTimeout()that throws instead of misbehaving with large delays
A value larger than 2147483647 milliseconds (~25 days) is too large to fit into a signed 32-bit integer, which is not supported by some JS engines, which will silently use 1 millisecond instead.
Such a surprising behavior is a very bad idea and has caused me issues a couple of times.
This module exports a small wrapper around setTimeout which will throw a RangeError if the specified delay is above this value.
Install
> npm install strict-timeoutCompatibility
Should be compatible with pretty much anything that provides a global setTimeout and clearTimeout:
- no dependencies
- ECMAScript 3
- universal module
Usage
The API is exactly the same as native
setTimeout
and the returned result can be used with native
clearTimeout,
the only difference is that it will throw if the delay is too big.
const strictTimeout = require("strict-timeout");
const handle = setTimeout(callback, delay, arg1, arg2);safeTimeout
This module also provides a safe version of setTimeout which will correctly behave with huge delays.
As for strictTimeout, the API is the same as native setTimeout, but you cannot use native clearTimeout for this one, you must use safeTimeout.clear() instead:
const safeTimeout = require("strict-timeout/safe");
const handle = safeTimeout(callback, delay, arg1, arg2);
// Node `ref` and `unref` methods are available
handle.unref();
handle.ref();
// you cannot use native clearTimeout!
safeTimeout.clear(handle);