What is this?
A library providing a parametrized random delay mechanism for functions (Both async and sync)
How do I install it?
You can install it by using the following command:
npm install random-delay
How do I use it?
Both sync and async functions are supported.
The library provides a randomDelay
function that takes 3 parameters:
-
minDelay
: The minimum delay in milliseconds. Defaults to0
. -
maxDelay
: The maximum delay in milliseconds. Defaults to1000
. -
method
: The method to use. Can be eithersync
orasync
. Defaults tosync
.
Sync
The library uses the event-loop-sleep
library to sleep the event loop using SharedArrayBuffer
and Atomics
for sync functions.
const { randomDelay } = require('random-delay');
// Do something before...
randomDelay(1000, 2000, 'sync');
// Do something after...
Async
The library uses a simple setTimeout
(unrefed) for async functions.
const { randomDelay } = require('random-delay');
// Do something before...
await randomDelay(1000, 2000, 'async');
// Do something after...
randomDelayed
The library also provides a randomDelayed
, a higher-order function that takes 2 parameters:
-
fn
: The function to wrap. -
options
: The options to pass torandomDelay
. Defaults to the same asrandomDelay
.
const { randomDelayed } = require('random-delay');
// Do something before...
await randomDelayed(() => {
// Do something
}, { minDelay: 1000, maxDelay: 2000, method: 'async' });
// Do something after...
Tests
You can run the tests by using the following command:
npm test
Top comments (0)