Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jul 18, 2020
1 parent aa0a2e4 commit d82de79
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
@@ -1,5 +1,7 @@
language: node_js
node_js:
- '14'
- '12'
- '10'
- '8'
- '6'
4 changes: 2 additions & 2 deletions index.d.ts
Expand Up @@ -75,13 +75,13 @@ type Delay = {
}
): delay.ClearablePromise<T>;

// TODO: Allow providing value type after https://github.com/Microsoft/TypeScript/issues/5413 is resolved.
/**
Create a promise which rejects after the specified `milliseconds`.
@param milliseconds - Milliseconds to delay the promise.
@returns A promise which rejects after the specified `milliseconds`.
*/
// TODO: Allow providing value type after https://github.com/Microsoft/TypeScript/issues/5413 will be resolved.
reject(
milliseconds: number,
options?: delay.Options & {
Expand All @@ -99,7 +99,7 @@ declare const delay: Delay & {
setTimeout: typeof setTimeout;
}): Delay;

// TODO: Remove this for the next major release
// TODO: Remove this for the next major release.
default: typeof delay;
};

Expand Down
2 changes: 1 addition & 1 deletion license
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -4,10 +4,11 @@
"description": "Delay a promise a specified amount of time",
"license": "MIT",
"repository": "sindresorhus/delay",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=6"
Expand Down
21 changes: 6 additions & 15 deletions readme.md
@@ -1,15 +1,13 @@
# delay [![Build Status](https://travis-ci.org/sindresorhus/delay.svg?branch=master)](https://travis-ci.org/sindresorhus/delay)
# delay [![Build Status](https://travis-ci.com/sindresorhus/delay.svg?branch=master)](https://travis-ci.com/github/sindresorhus/delay)

> Delay a promise a specified amount of time

## Install

```
$ npm install delay
```


## Usage

```js
Expand All @@ -25,18 +23,17 @@ const delay = require('delay');
})();
```


## API

### delay(milliseconds, [options])
### delay(milliseconds, options?)

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

### delay.reject(milliseconds, [options])
### delay.reject(milliseconds, options?)

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

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

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

Expand All @@ -52,11 +49,11 @@ Milliseconds to delay the promise.

#### options

Type: `Object`
Type: `object`

##### value

Type: `any`
Type: `unknown`

Optional value to resolve or reject in the returned promise.

Expand Down Expand Up @@ -164,7 +161,6 @@ const customDelay = delay.createWithTimers({clearTimeout, setTimeout});
})();
```


## Related

- [delay-cli](https://github.com/sindresorhus/delay-cli) - CLI for this module
Expand All @@ -173,8 +169,3 @@ const customDelay = delay.createWithTimers({clearTimeout, setTimeout});
- [p-immediate](https://github.com/sindresorhus/p-immediate) - Returns a promise resolved in the next event loop - think `setImmediate()`
- [p-timeout](https://github.com/sindresorhus/p-timeout) - Timeout a promise after a specified amount of time
- [More…](https://github.com/sindresorhus/promise-fun)


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
34 changes: 17 additions & 17 deletions test.js
Expand Up @@ -3,67 +3,67 @@ import timeSpan from 'time-span';
import inRange from 'in-range';
import currentlyUnhandled from 'currently-unhandled';
import {AbortController} from 'abort-controller';
import m from '.';
import delay from '.';

const getCurrentlyUnhandled = currentlyUnhandled();

test('returns a resolved promise', async t => {
const end = timeSpan();
await m(50);
await delay(50);
t.true(inRange(end(), 30, 70), 'is delayed');
});

test('returns a rejected promise', async t => {
const end = timeSpan();
await t.throwsAsync(
m.reject(50, {value: new Error('foo')}),
delay.reject(50, {value: new Error('foo')}),
'foo'
);
t.true(inRange(end(), 30, 70), 'is delayed');
});

test('able to resolve a falsy value', async t => {
t.is(
await m(50, {value: 0}),
await delay(50, {value: 0}),
0
);
});

test('able to reject a falsy value', async t => {
t.plan(1);
try {
await m.reject(50, {value: false});
await delay.reject(50, {value: false});
} catch (error) {
t.is(error, false);
}
});

test('delay defaults to 0 ms', async t => {
const end = timeSpan();
await m();
await delay();
t.true(end() < 30);
});

test('reject will cause an unhandledRejection if not caught', async t => {
const reason = new Error('foo');
const promise = m.reject(0, {value: reason});
const promise = delay.reject(0, {value: reason});

await m(10);
await delay(10);

t.deepEqual(getCurrentlyUnhandled(), [{
reason,
promise
}], 'Promise should be unhandled');

promise.catch(() => {});
await m(10);
await delay(10);

t.deepEqual(getCurrentlyUnhandled(), [], 'no unhandled rejections now');
});

test('can clear a delayed resolution', async t => {
const end = timeSpan();
const delayPromise = m(1000, {value: 'success!'});
const delayPromise = delay(1000, {value: 'success!'});

delayPromise.clear();
const success = await delayPromise;
Expand All @@ -74,7 +74,7 @@ test('can clear a delayed resolution', async t => {

test('can clear a delayed rejection', async t => {
const end = timeSpan();
const delayPromise = m.reject(1000, {value: new Error('error!')});
const delayPromise = delay.reject(1000, {value: new Error('error!')});
delayPromise.clear();

await t.throwsAsync(delayPromise, /error!/);
Expand All @@ -86,7 +86,7 @@ test('resolution can be aborted with an AbortSignal', async t => {
const abortController = new AbortController();
setTimeout(() => abortController.abort(), 1);
await t.throwsAsync(
m(1000, {signal: abortController.signal}),
delay(1000, {signal: abortController.signal}),
{name: 'AbortError'}
);
t.true(end() < 30);
Expand All @@ -97,7 +97,7 @@ test('resolution can be aborted with an AbortSignal if a value is passed', async
const abortController = new AbortController();
setTimeout(() => abortController.abort(), 1);
await t.throwsAsync(
m(1000, {value: 123, signal: abortController.signal}),
delay(1000, {value: 123, signal: abortController.signal}),
{name: 'AbortError'}
);
t.true(end() < 30);
Expand All @@ -108,7 +108,7 @@ test('rejection can be aborted with an AbortSignal if a value is passed', async
const abortController = new AbortController();
setTimeout(() => abortController.abort(), 1);
await t.throwsAsync(
m.reject(1000, {value: new Error(), signal: abortController.signal}),
delay.reject(1000, {value: new Error(), signal: abortController.signal}),
{name: 'AbortError'}
);
t.true(end() < 30);
Expand All @@ -119,7 +119,7 @@ test('rejects with AbortError if AbortSignal is already aborted', async t => {
const abortController = new AbortController();
abortController.abort();
await t.throwsAsync(
m(1000, {signal: abortController.signal}),
delay(1000, {signal: abortController.signal}),
{name: 'AbortError'}
);
t.true(end() < 30);
Expand All @@ -128,7 +128,7 @@ test('rejects with AbortError if AbortSignal is already aborted', async t => {
test('can create a new instance with fixed timeout methods', async t => {
const cleared = [];
const callbacks = [];
const custom = m.createWithTimers({
const custom = delay.createWithTimers({
clearTimeout(handle) {
cleared.push(handle);
},
Expand Down Expand Up @@ -166,6 +166,6 @@ test('can create a new instance with fixed timeout methods', async t => {

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

0 comments on commit d82de79

Please sign in to comment.