├── .editorconfig ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 18 14 | - 16 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export class TimeoutError extends Error { 2 | readonly name: 'TimeoutError'; 3 | constructor(message?: string); 4 | } 5 | 6 | export type ClearablePromise = { 7 | /** 8 | Clear the timeout. 9 | */ 10 | clear: () => void; 11 | } & Promise; 12 | 13 | export type Options = { 14 | /** 15 | Milliseconds before timing out. 16 | 17 | Passing `Infinity` will cause it to never time out. 18 | */ 19 | milliseconds: number; 20 | 21 | /** 22 | Do something other than rejecting with an error on timeout. 23 | 24 | You could for example retry: 25 | 26 | @example 27 | ``` 28 | import {setTimeout} from 'node:timers/promises'; 29 | import pTimeout from 'p-timeout'; 30 | 31 | const delayedPromise = () => setTimeout(200); 32 | 33 | await pTimeout(delayedPromise(), { 34 | milliseconds: 50, 35 | fallback: () => { 36 | return pTimeout(delayedPromise(), { 37 | milliseconds: 300 38 | }); 39 | }, 40 | }); 41 | ``` 42 | */ 43 | fallback?: () => ReturnType | Promise; 44 | 45 | /** 46 | Specify a custom error message or error to throw when it times out: 47 | 48 | - `message: 'too slow'` will throw `TimeoutError('too slow')` 49 | - `message: new MyCustomError('it’s over 9000')` will throw the same error instance 50 | - `message: false` will make the promise resolve with `undefined` instead of rejecting 51 | 52 | If you do a custom error, it's recommended to sub-class `TimeoutError`: 53 | 54 | ``` 55 | import {TimeoutError} from 'p-timeout'; 56 | 57 | class MyCustomError extends TimeoutError { 58 | name = "MyCustomError"; 59 | } 60 | ``` 61 | */ 62 | message?: string | Error | false; 63 | 64 | /** 65 | Custom implementations for the `setTimeout` and `clearTimeout` functions. 66 | 67 | Useful for testing purposes, in particular to work around [`sinon.useFakeTimers()`](https://sinonjs.org/releases/latest/fake-timers/). 68 | 69 | @example 70 | ``` 71 | import pTimeout from 'p-timeout'; 72 | import sinon from 'sinon'; 73 | 74 | const originalSetTimeout = setTimeout; 75 | const originalClearTimeout = clearTimeout; 76 | 77 | sinon.useFakeTimers(); 78 | 79 | // Use `pTimeout` without being affected by `sinon.useFakeTimers()`: 80 | await pTimeout(doSomething(), { 81 | milliseconds: 2000, 82 | customTimers: { 83 | setTimeout: originalSetTimeout, 84 | clearTimeout: originalClearTimeout 85 | } 86 | }); 87 | ``` 88 | */ 89 | readonly customTimers?: { 90 | setTimeout: typeof globalThis.setTimeout; 91 | clearTimeout: typeof globalThis.clearTimeout; 92 | }; 93 | 94 | /** 95 | You can abort the promise using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). 96 | 97 | _Requires Node.js 16 or later._ 98 | 99 | @example 100 | ``` 101 | import pTimeout from 'p-timeout'; 102 | import delay from 'delay'; 103 | 104 | const delayedPromise = delay(3000); 105 | 106 | const abortController = new AbortController(); 107 | 108 | setTimeout(() => { 109 | abortController.abort(); 110 | }, 100); 111 | 112 | await pTimeout(delayedPromise, { 113 | milliseconds: 2000, 114 | signal: abortController.signal 115 | }); 116 | ``` 117 | */ 118 | signal?: globalThis.AbortSignal; 119 | }; 120 | 121 | /** 122 | Timeout a promise after a specified amount of time. 123 | 124 | If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out. 125 | 126 | @param input - Promise to decorate. 127 | @returns A decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout. 128 | 129 | @example 130 | ``` 131 | import {setTimeout} from 'node:timers/promises'; 132 | import pTimeout from 'p-timeout'; 133 | 134 | const delayedPromise = () => setTimeout(200); 135 | 136 | await pTimeout(delayedPromise(), { 137 | milliseconds: 50, 138 | fallback: () => { 139 | return pTimeout(delayedPromise(), {milliseconds: 300}); 140 | } 141 | }); 142 | ``` 143 | */ 144 | export default function pTimeout( 145 | input: PromiseLike, 146 | options: Options & {message: false} 147 | ): ClearablePromise; 148 | export default function pTimeout( 149 | input: PromiseLike, 150 | options: Options 151 | ): ClearablePromise; 152 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export class TimeoutError extends Error { 2 | constructor(message) { 3 | super(message); 4 | this.name = 'TimeoutError'; 5 | } 6 | } 7 | 8 | /** 9 | An error to be thrown when the request is aborted by AbortController. 10 | DOMException is thrown instead of this Error when DOMException is available. 11 | */ 12 | export class AbortError extends Error { 13 | constructor(message) { 14 | super(); 15 | this.name = 'AbortError'; 16 | this.message = message; 17 | } 18 | } 19 | 20 | /** 21 | TODO: Remove AbortError and just throw DOMException when targeting Node 18. 22 | */ 23 | const getDOMException = errorMessage => globalThis.DOMException === undefined 24 | ? new AbortError(errorMessage) 25 | : new DOMException(errorMessage); 26 | 27 | /** 28 | TODO: Remove below function and just 'reject(signal.reason)' when targeting Node 18. 29 | */ 30 | const getAbortedReason = signal => { 31 | const reason = signal.reason === undefined 32 | ? getDOMException('This operation was aborted.') 33 | : signal.reason; 34 | 35 | return reason instanceof Error ? reason : getDOMException(reason); 36 | }; 37 | 38 | export default function pTimeout(promise, options) { 39 | const { 40 | milliseconds, 41 | fallback, 42 | message, 43 | customTimers = {setTimeout, clearTimeout}, 44 | } = options; 45 | 46 | let timer; 47 | let abortHandler; 48 | 49 | const wrappedPromise = new Promise((resolve, reject) => { 50 | if (typeof milliseconds !== 'number' || Math.sign(milliseconds) !== 1) { 51 | throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``); 52 | } 53 | 54 | if (options.signal) { 55 | const {signal} = options; 56 | if (signal.aborted) { 57 | reject(getAbortedReason(signal)); 58 | } 59 | 60 | abortHandler = () => { 61 | reject(getAbortedReason(signal)); 62 | }; 63 | 64 | signal.addEventListener('abort', abortHandler, {once: true}); 65 | } 66 | 67 | if (milliseconds === Number.POSITIVE_INFINITY) { 68 | promise.then(resolve, reject); 69 | return; 70 | } 71 | 72 | // We create the error outside of `setTimeout` to preserve the stack trace. 73 | const timeoutError = new TimeoutError(); 74 | 75 | timer = customTimers.setTimeout.call(undefined, () => { 76 | if (fallback) { 77 | try { 78 | resolve(fallback()); 79 | } catch (error) { 80 | reject(error); 81 | } 82 | 83 | return; 84 | } 85 | 86 | if (typeof promise.cancel === 'function') { 87 | promise.cancel(); 88 | } 89 | 90 | if (message === false) { 91 | resolve(); 92 | } else if (message instanceof Error) { 93 | reject(message); 94 | } else { 95 | timeoutError.message = message ?? `Promise timed out after ${milliseconds} milliseconds`; 96 | reject(timeoutError); 97 | } 98 | }, milliseconds); 99 | 100 | (async () => { 101 | try { 102 | resolve(await promise); 103 | } catch (error) { 104 | reject(error); 105 | } 106 | })(); 107 | }); 108 | 109 | const cancelablePromise = wrappedPromise.finally(() => { 110 | cancelablePromise.clear(); 111 | if (abortHandler && options.signal) { 112 | options.signal.removeEventListener('abort', abortHandler); 113 | } 114 | }); 115 | 116 | cancelablePromise.clear = () => { 117 | customTimers.clearTimeout.call(undefined, timer); 118 | timer = undefined; 119 | }; 120 | 121 | return cancelablePromise; 122 | } 123 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable @typescript-eslint/no-floating-promises */ 2 | /* eslint-disable unicorn/prefer-top-level-await */ 3 | import {expectType, expectError} from 'tsd'; 4 | import pTimeout, {TimeoutError} from './index.js'; 5 | 6 | const delayedPromise: () => Promise = async () => new Promise(resolve => { 7 | setTimeout(() => { 8 | resolve('foo'); 9 | }, 200); 10 | }); 11 | 12 | pTimeout(delayedPromise(), {milliseconds: 50}).then(() => 'foo'); 13 | pTimeout(delayedPromise(), {milliseconds: 50, fallback: async () => pTimeout(delayedPromise(), {milliseconds: 300})}); 14 | pTimeout(delayedPromise(), {milliseconds: 50}).then(value => { 15 | expectType(value); 16 | }); 17 | pTimeout(delayedPromise(), {milliseconds: 50, message: 'error'}).then(value => { 18 | expectType(value); 19 | }); 20 | pTimeout(delayedPromise(), {milliseconds: 50, message: false}).then(value => { 21 | expectType(value); 22 | }); 23 | pTimeout(delayedPromise(), {milliseconds: 50, message: new Error('error')}).then(value => { 24 | expectType(value); 25 | }); 26 | pTimeout(delayedPromise(), {milliseconds: 50, fallback: async () => 10}).then(value => { 27 | expectType(value); 28 | }); 29 | pTimeout(delayedPromise(), {milliseconds: 50, fallback: () => 10}).then(value => { 30 | expectType(value); 31 | }); 32 | 33 | const customTimers = {setTimeout, clearTimeout}; 34 | pTimeout(delayedPromise(), {milliseconds: 50, customTimers}); 35 | pTimeout(delayedPromise(), {milliseconds: 50, message: 'foo', customTimers}); 36 | pTimeout(delayedPromise(), {milliseconds: 50, message: new Error('error'), customTimers}); 37 | pTimeout(delayedPromise(), {milliseconds: 50, fallback: () => 10}); 38 | 39 | expectError(pTimeout(delayedPromise(), { 40 | milliseconds: 50, 41 | fallback: () => 10, 42 | customTimers: { 43 | setTimeout, 44 | }, 45 | })); 46 | 47 | expectError(pTimeout(delayedPromise(), { 48 | milliseconds: 50, 49 | fallback: () => 10, 50 | customTimers: { 51 | setTimeout: () => 42, // Invalid `setTimeout` implementation 52 | clearTimeout, 53 | }, 54 | })); 55 | 56 | expectError(pTimeout(delayedPromise(), {})); // `milliseconds` is required 57 | 58 | const timeoutError = new TimeoutError(); 59 | expectType(timeoutError); 60 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "p-timeout", 3 | "version": "6.1.4", 4 | "description": "Timeout a promise after a specified amount of time", 5 | "license": "MIT", 6 | "repository": "sindresorhus/p-timeout", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "types": "./index.d.ts", 16 | "sideEffects": false, 17 | "engines": { 18 | "node": ">=14.16" 19 | }, 20 | "scripts": { 21 | "test": "xo && ava && tsd" 22 | }, 23 | "files": [ 24 | "index.js", 25 | "index.d.ts" 26 | ], 27 | "keywords": [ 28 | "promise", 29 | "timeout", 30 | "error", 31 | "invalidate", 32 | "async", 33 | "await", 34 | "promises", 35 | "time", 36 | "out", 37 | "cancel", 38 | "bluebird" 39 | ], 40 | "devDependencies": { 41 | "ava": "^4.3.1", 42 | "delay": "^5.0.0", 43 | "in-range": "^3.0.0", 44 | "p-cancelable": "^4.0.1", 45 | "sinon": "^19.0.2", 46 | "time-span": "^5.1.0", 47 | "tsd": "^0.22.0", 48 | "xo": "^0.54.2" 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # p-timeout 2 | 3 | > Timeout a promise after a specified amount of time 4 | 5 | > [!NOTE] 6 | > You may want to use `AbortSignal.timeout()` instead. [Learn more.](#abortsignal) 7 | 8 | ## Install 9 | 10 | ```sh 11 | npm install p-timeout 12 | ``` 13 | 14 | ## Usage 15 | 16 | ```js 17 | import {setTimeout} from 'node:timers/promises'; 18 | import pTimeout from 'p-timeout'; 19 | 20 | const delayedPromise = setTimeout(200); 21 | 22 | await pTimeout(delayedPromise, { 23 | milliseconds: 50, 24 | }); 25 | //=> [TimeoutError: Promise timed out after 50 milliseconds] 26 | ``` 27 | 28 | ## API 29 | 30 | ### pTimeout(input, options) 31 | 32 | Returns a decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout. 33 | 34 | If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out. 35 | 36 | #### input 37 | 38 | Type: `Promise` 39 | 40 | Promise to decorate. 41 | 42 | #### options 43 | 44 | Type: `object` 45 | 46 | ##### milliseconds 47 | 48 | Type: `number` 49 | 50 | Milliseconds before timing out. 51 | 52 | Passing `Infinity` will cause it to never time out. 53 | 54 | ##### message 55 | 56 | Type: `string | Error | false`\ 57 | Default: `'Promise timed out after 50 milliseconds'` 58 | 59 | Specify a custom error message or error to throw when it times out: 60 | 61 | - `message: 'too slow'` will throw `TimeoutError('too slow')` 62 | - `message: new MyCustomError('it’s over 9000')` will throw the same error instance 63 | - `message: false` will make the promise resolve with `undefined` instead of rejecting 64 | 65 | If you do a custom error, it's recommended to sub-class `TimeoutError`: 66 | 67 | ```js 68 | import {TimeoutError} from 'p-timeout'; 69 | 70 | class MyCustomError extends TimeoutError { 71 | name = "MyCustomError"; 72 | } 73 | ``` 74 | 75 | ##### fallback 76 | 77 | Type: `Function` 78 | 79 | Do something other than rejecting with an error on timeout. 80 | 81 | You could for example retry: 82 | 83 | ```js 84 | import {setTimeout} from 'node:timers/promises'; 85 | import pTimeout from 'p-timeout'; 86 | 87 | const delayedPromise = () => setTimeout(200); 88 | 89 | await pTimeout(delayedPromise(), { 90 | milliseconds: 50, 91 | fallback: () => { 92 | return pTimeout(delayedPromise(), {milliseconds: 300}); 93 | }, 94 | }); 95 | ``` 96 | 97 | ##### customTimers 98 | 99 | Type: `object` with function properties `setTimeout` and `clearTimeout` 100 | 101 | Custom implementations for the `setTimeout` and `clearTimeout` functions. 102 | 103 | Useful for testing purposes, in particular to work around [`sinon.useFakeTimers()`](https://sinonjs.org/releases/latest/fake-timers/). 104 | 105 | Example: 106 | 107 | ```js 108 | import {setTimeout} from 'node:timers/promises'; 109 | import pTimeout from 'p-timeout'; 110 | 111 | const originalSetTimeout = setTimeout; 112 | const originalClearTimeout = clearTimeout; 113 | 114 | sinon.useFakeTimers(); 115 | 116 | // Use `pTimeout` without being affected by `sinon.useFakeTimers()`: 117 | await pTimeout(doSomething(), { 118 | milliseconds: 2000, 119 | customTimers: { 120 | setTimeout: originalSetTimeout, 121 | clearTimeout: originalClearTimeout 122 | } 123 | }); 124 | ``` 125 | 126 | #### signal 127 | 128 | Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) 129 | 130 | You can abort the promise using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). 131 | 132 | *Requires Node.js 16 or later.* 133 | 134 | ```js 135 | import pTimeout from 'p-timeout'; 136 | import delay from 'delay'; 137 | 138 | const delayedPromise = delay(3000); 139 | 140 | const abortController = new AbortController(); 141 | 142 | setTimeout(() => { 143 | abortController.abort(); 144 | }, 100); 145 | 146 | await pTimeout(delayedPromise, { 147 | milliseconds: 2000, 148 | signal: abortController.signal 149 | }); 150 | ``` 151 | 152 | ### TimeoutError 153 | 154 | Exposed for instance checking and sub-classing. 155 | 156 | ## Related 157 | 158 | - [delay](https://github.com/sindresorhus/delay) - Delay a promise a specified amount of time 159 | - [p-min-delay](https://github.com/sindresorhus/p-min-delay) - Delay a promise a minimum amount of time 160 | - [p-retry](https://github.com/sindresorhus/p-retry) - Retry a promise-returning function 161 | - [More…](https://github.com/sindresorhus/promise-fun) 162 | 163 | ## AbortSignal 164 | 165 | > Modern alternative to `p-timeout` 166 | 167 | Asynchronous functions like `fetch` can accept an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal), which can be conveniently created with [`AbortSignal.timeout()`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static). 168 | 169 | The advantage over `p-timeout` is that the promise-generating function (like `fetch`) is actually notified that the user is no longer expecting an answer, so it can interrupt its work and free resources. 170 | 171 | ```js 172 | // Call API, timeout after 5 seconds 173 | const response = await fetch('./my-api', {signal: AbortSignal.timeout(5000)}); 174 | ``` 175 | 176 | ```js 177 | async function buildWall(signal) { 178 | for (const brick of bricks) { 179 | signal.throwIfAborted(); 180 | // Or: if (signal.aborted) { return; } 181 | 182 | await layBrick(); 183 | } 184 | } 185 | 186 | // Stop long work after 60 seconds 187 | await buildWall(AbortSignal.timeout(60_000)) 188 | ``` 189 | 190 | You can also combine multiple signals, like when you have a timeout *and* an `AbortController` triggered with a “Cancel” button click. You can use the upcoming [`AbortSignal.any()`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static) helper or [`abort-utils`](https://github.com/fregante/abort-utils/blob/main/source/merge-signals.md). 191 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import delay from 'delay'; 3 | import PCancelable from 'p-cancelable'; 4 | import inRange from 'in-range'; 5 | import timeSpan from 'time-span'; 6 | import sinon from 'sinon'; 7 | import pTimeout, {TimeoutError} from './index.js'; 8 | 9 | const fixture = Symbol('fixture'); 10 | const fixtureError = new Error('fixture'); 11 | 12 | test('resolves before timeout', async t => { 13 | t.is(await pTimeout(delay(50).then(() => fixture), {milliseconds: 200}), fixture); 14 | }); 15 | 16 | test('throws when milliseconds is not number', async t => { 17 | await t.throwsAsync(pTimeout(delay(50), {milliseconds: '200'}), {instanceOf: TypeError}); 18 | }); 19 | 20 | test('throws when milliseconds is negative number', async t => { 21 | await t.throwsAsync(pTimeout(delay(50), {milliseconds: -1}), {instanceOf: TypeError}); 22 | }); 23 | 24 | test('throws when milliseconds is NaN', async t => { 25 | await t.throwsAsync(pTimeout(delay(50), {milliseconds: Number.NaN}), {instanceOf: TypeError}); 26 | }); 27 | 28 | test('handles milliseconds being `Infinity`', async t => { 29 | t.is( 30 | await pTimeout(delay(50, {value: fixture}), {milliseconds: Number.POSITIVE_INFINITY}), 31 | fixture, 32 | ); 33 | }); 34 | 35 | test('rejects after timeout', async t => { 36 | await t.throwsAsync(pTimeout(delay(200), {milliseconds: 50}), {instanceOf: TimeoutError}); 37 | }); 38 | 39 | test('resolves after timeout with message:false', async t => { 40 | t.is( 41 | await pTimeout(delay(200), {milliseconds: 50, message: false}), 42 | undefined, 43 | ); 44 | }); 45 | 46 | test('rejects before timeout if specified promise rejects', async t => { 47 | await t.throwsAsync(pTimeout(delay(50).then(() => { 48 | throw fixtureError; 49 | }), {milliseconds: 200}), {message: fixtureError.message}); 50 | }); 51 | 52 | test('fallback argument', async t => { 53 | await t.throwsAsync(pTimeout(delay(200), {milliseconds: 50, message: 'rainbow'}), {message: 'rainbow'}); 54 | await t.throwsAsync(pTimeout(delay(200), {milliseconds: 50, message: new RangeError('cake')}), {instanceOf: RangeError}); 55 | await t.throwsAsync(pTimeout(delay(200), {milliseconds: 50, fallback: () => Promise.reject(fixtureError)}), {message: fixtureError.message}); 56 | await t.throwsAsync(pTimeout(delay(200), {milliseconds: 50, fallback() { 57 | throw new RangeError('cake'); 58 | }}), {instanceOf: RangeError}); 59 | }); 60 | 61 | test('calls `.cancel()` on promise when it exists', async t => { 62 | const promise = new PCancelable(async (resolve, reject, onCancel) => { 63 | onCancel(() => { 64 | t.pass(); 65 | }); 66 | 67 | await delay(200); 68 | resolve(); 69 | }); 70 | 71 | await t.throwsAsync(pTimeout(promise, {milliseconds: 50}), {instanceOf: TimeoutError}); 72 | t.true(promise.isCanceled); 73 | }); 74 | 75 | test('accepts `customTimers` option', async t => { 76 | t.plan(2); 77 | 78 | await pTimeout(delay(50), { 79 | milliseconds: 123, 80 | customTimers: { 81 | setTimeout(fn, milliseconds) { 82 | t.is(milliseconds, 123); 83 | return setTimeout(fn, milliseconds); 84 | }, 85 | clearTimeout(timeoutId) { 86 | t.pass(); 87 | return clearTimeout(timeoutId); 88 | }, 89 | }, 90 | }); 91 | }); 92 | 93 | test('`.clear()` method', async t => { 94 | const end = timeSpan(); 95 | const promise = pTimeout(delay(300), {milliseconds: 200}); 96 | 97 | promise.clear(); 98 | 99 | await promise; 100 | t.true(inRange(end(), {start: 0, end: 350})); 101 | }); 102 | 103 | /** 104 | TODO: Remove if statement when targeting Node.js 16. 105 | */ 106 | if (globalThis.AbortController !== undefined) { 107 | test('rejects when calling `AbortController#abort()`', async t => { 108 | const abortController = new AbortController(); 109 | 110 | const promise = pTimeout(delay(3000), { 111 | milliseconds: 2000, 112 | signal: abortController.signal, 113 | }); 114 | 115 | abortController.abort(); 116 | 117 | await t.throwsAsync(promise, { 118 | name: 'AbortError', 119 | }); 120 | }); 121 | 122 | test('already aborted signal', async t => { 123 | const abortController = new AbortController(); 124 | 125 | abortController.abort(); 126 | 127 | await t.throwsAsync(pTimeout(delay(3000), { 128 | milliseconds: 2000, 129 | signal: abortController.signal, 130 | }), { 131 | name: 'AbortError', 132 | }); 133 | }); 134 | 135 | test('aborts even if milliseconds are set to infinity', async t => { 136 | const abortController = new AbortController(); 137 | 138 | abortController.abort(); 139 | 140 | await t.throwsAsync(pTimeout(delay(3000), { 141 | milliseconds: Number.POSITIVE_INFINITY, 142 | signal: abortController.signal, 143 | }), { 144 | name: 'AbortError', 145 | }); 146 | }); 147 | 148 | test('removes abort listener after promise settles', async t => { 149 | const abortController = new AbortController(); 150 | const {signal} = abortController; 151 | 152 | const addEventListenerSpy = sinon.spy(signal, 'addEventListener'); 153 | const removeEventListenerSpy = sinon.spy(signal, 'removeEventListener'); 154 | 155 | const promise = pTimeout(delay(50), { 156 | milliseconds: 100, 157 | signal, 158 | }); 159 | 160 | await promise; 161 | 162 | t.true(addEventListenerSpy.calledWith('abort'), 'addEventListener should be called with "abort"'); 163 | t.true(removeEventListenerSpy.calledWith('abort'), 'removeEventListener should be called with "abort"'); 164 | 165 | addEventListenerSpy.restore(); 166 | removeEventListenerSpy.restore(); 167 | }); 168 | 169 | test('removes abort listener after promise rejects', async t => { 170 | const abortController = new AbortController(); 171 | const {signal} = abortController; 172 | 173 | const addEventListenerSpy = sinon.spy(signal, 'addEventListener'); 174 | const removeEventListenerSpy = sinon.spy(signal, 'removeEventListener'); 175 | 176 | const promise = pTimeout( 177 | (async () => { 178 | await delay(50); 179 | throw new Error('Test error'); 180 | })(), 181 | { 182 | milliseconds: 100, 183 | signal, 184 | }, 185 | ); 186 | 187 | await t.throwsAsync(promise, {message: 'Test error'}); 188 | 189 | t.true(addEventListenerSpy.calledWith('abort'), 'addEventListener should be called with "abort"'); 190 | t.true(removeEventListenerSpy.calledWith('abort'), 'removeEventListener should be called with "abort"'); 191 | 192 | addEventListenerSpy.restore(); 193 | removeEventListenerSpy.restore(); 194 | }); 195 | } 196 | --------------------------------------------------------------------------------