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

Fix missing range() in createWithTimers() return value & require Node.js 10 #55

Merged
merged 2 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ jobs:
- 14
- 12
- 10
- 8
- 6
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ const createDelay = ({clearTimeout: defaultClear, setTimeout: set, willResolve})
return delayPromise;
};

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});
const createWithTimers = clearAndSet => {
const delay = createDelay({...clearAndSet, willResolve: true});
delay.reject = createDelay({...clearAndSet, willResolve: false});
delay.range = (minimum, maximum, options) => delay(randomInteger(minimum, maximum), options);
return delay;
};

const delay = createWithTimers();
delay.createWithTimers = createWithTimers;

module.exports = delay;
// TODO: Remove this for the next major release
module.exports.default = delay;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down
16 changes: 11 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ test('rejects with AbortError if AbortSignal is already aborted', async t => {
t.true(end() < 30);
});

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

test('can create a new instance with fixed timeout methods', async t => {
const cleared = [];
const callbacks = [];
Expand Down Expand Up @@ -162,10 +168,10 @@ test('can create a new instance with fixed timeout methods', async t => {
third.clear();
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 delay.range(50, 150);
t.true(inRange(end(), 30, 170), 'is delayed');
const fourth = custom.range(50, 150, {value: 'fourth'});
t.is(callbacks.length, 4);
t.true(inRange(callbacks[2].ms, 50, 150));
callbacks[3].callback();
t.is(await fourth, 'fourth');
});