diff --git a/.travis.yml b/.travis.yml index 2ae9d62..81f3378 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: node_js node_js: + - '14' + - '12' - '10' - '8' - '6' diff --git a/index.d.ts b/index.d.ts index 8b45222..ce5ad7e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -75,13 +75,13 @@ type Delay = { } ): delay.ClearablePromise; + // 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 & { @@ -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; }; diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (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: diff --git a/package.json b/package.json index f7e03ee..24cf8c1 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/readme.md b/readme.md index 9a55098..1583eef 100644 --- a/readme.md +++ b/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 @@ -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. @@ -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. @@ -164,7 +161,6 @@ const customDelay = delay.createWithTimers({clearTimeout, setTimeout}); })(); ``` - ## Related - [delay-cli](https://github.com/sindresorhus/delay-cli) - CLI for this module @@ -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) diff --git a/test.js b/test.js index fec6caa..0660133 100644 --- a/test.js +++ b/test.js @@ -3,20 +3,20 @@ 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'); @@ -24,7 +24,7 @@ test('returns a rejected promise', async t => { test('able to resolve a falsy value', async t => { t.is( - await m(50, {value: 0}), + await delay(50, {value: 0}), 0 ); }); @@ -32,7 +32,7 @@ test('able to resolve a falsy value', async t => { 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); } @@ -40,15 +40,15 @@ test('able to reject a falsy value', async t => { 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, @@ -56,14 +56,14 @@ test('reject will cause an unhandledRejection if not caught', async t => { }], '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; @@ -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!/); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); }, @@ -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'); });