Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .range() method #51

Merged
merged 16 commits into from Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions index.d.ts
Expand Up @@ -55,6 +55,24 @@ type Delay = {
}
): delay.ClearablePromise<T>;

/**
Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed.

Richienb marked this conversation as resolved.
Show resolved Hide resolved
@param minimum - Minimum amount of milliseconds to delay the promise.
@param maximum - Maximum amount of milliseconds to delay the promise.
@returns A promise which resolves after a random amount of milliseconds between `maximum` and `maximum` has passed.
*/
range<T>(
minimum: number,
maximum: number,
options?: delay.Options & {
/**
Value to resolve in the returned promise.
*/
value: T;
}
): delay.ClearablePromise<T>;

/**
Create a promise which rejects after the specified `milliseconds`.

Expand Down
2 changes: 2 additions & 0 deletions index.js
@@ -1,4 +1,5 @@
'use strict';
const randomInteger = require('random-int');

const createAbortError = () => {
const error = new Error('Delay aborted');
Expand Down Expand Up @@ -56,6 +57,7 @@ const createDelay = ({clearTimeout: defaultClear, setTimeout: set, willResolve})

const delay = createDelay({willResolve: true});
delay.reject = createDelay({willResolve: false});
delay.range = (minimum, maximum, options) => delay(randomInteger(minimum, maximum), options);
delay.createWithTimers = ({clearTimeout, setTimeout}) => {
const delay = createDelay({clearTimeout, setTimeout, willResolve: true});
delay.reject = createDelay({clearTimeout, setTimeout, willResolve: false});
Expand Down
2 changes: 2 additions & 0 deletions index.test-d.ts
Expand Up @@ -11,6 +11,8 @@ expectType<ClearablePromise<void>>(
delay(200, {signal: new AbortController().signal})
);

expectType<ClearablePromise<number>>(delay.range(50, 200, { value: 0 }));

expectType<ClearablePromise<never>>(delay.reject(200, {value: '🦄'}));
expectType<ClearablePromise<never>>(delay.reject(200, {value: 0}));

Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -38,6 +38,9 @@
"promises",
"bluebird"
],
"dependencies": {
"random-int": "^2.0.1"
Richienb marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"abort-controller": "^3.0.0",
"ava": "1.4.1",
Expand Down
6 changes: 5 additions & 1 deletion readme.md
Expand Up @@ -36,7 +36,11 @@ Create a promise which resolves after the specified `milliseconds`.

Create a promise which rejects after the specified `milliseconds`.

#### milliseconds
### delay.range(minimum, maximum, [options])

Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed.

#### milliseconds (minimum and maximum for delay.range)
Richienb marked this conversation as resolved.
Show resolved Hide resolved

Type: `number`

Expand Down
6 changes: 6 additions & 0 deletions test.js
Expand Up @@ -163,3 +163,9 @@ test('can create a new instance with fixed timeout methods', async t => {
t.is(cleared.length, 1);
t.is(cleared[0], callbacks[2].handle);
});

test('returns a promise that is resolved in a random range of time', async t => {
const end = timeSpan();
await m.range(50, 150);
t.true(inRange(end(), 30, 170), 'is delayed');
});