{"name":"p-timeout","version":"8.0.0","keywords":["promise","timeout","error","invalidate","async","await","promises","time","out","cancel","bluebird"],"author":{"url":"https://sindresorhus.com","name":"Sindre Sorhus","email":"sindresorhus@gmail.com"},"license":"MIT","_id":"p-timeout@8.0.0","maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"homepage":"https://github.com/sindresorhus/p-timeout#readme","bugs":{"url":"https://github.com/sindresorhus/p-timeout/issues"},"xo":[{"rules":{"node-test/prefer-test-context-assert":"off"}}],"dist":{"shasum":"90adbd066c2cef50fca3c37a7dd7480163a95a24","tarball":"http://registry-npm.ywwl.com/p-timeout/download/p-timeout-8.0.0.tgz","fileCount":5,"integrity":"sha512-IHjMTvwSvMPKVfGAVTRxI+Ea6Vu5soB0oaC3fuEc58EI01T36AiVclDTTYoHBO8M1gjxqAvgkRLm33TGUCzEDw==","signatures":[{"sig":"MEYCIQDU2QVGoKs0lrUxAW6T3IGNxK/8lO8SFnGSzl72WrprzQIhALBYZ7QW1UIBAMm1sZltfoiGbMX29BY8HFx0MwFr5AUg","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"sig":"MEYCIQDsx0zSaLiIMBw/O/uBAlDGyfxyu06gtiS0LRPB2VC2rAIhAPeIsjdyyIwA0h3T1169OqSV3BfS1bRiIDG8hB4jsRNk","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":16768,"size":5829,"noattachment":false,"key":"/p-timeout/-/p-timeout-8.0.0.tgz"},"type":"module","types":"./index.d.ts","engines":{"node":">=22"},"exports":{".":{"types":"./index.d.ts","default":"./index.js"}},"funding":"https://github.com/sponsors/sindresorhus","gitHead":"071659ceb486e8ef960a1ddadefcc946fc5e309b","scripts":{"test":"xo && node --test && nz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"repository":{"url":"git+https://github.com/sindresorhus/p-timeout.git","type":"git"},"_npmVersion":"12.2.0","description":"Timeout a promise after a specified amount of time","directories":{},"sideEffects":false,"_nodeVersion":"26.11.1","_hasShrinkwrap":false,"devDependencies":{"nz":"^0.1.0","xo":"^5.0.1","delay":"^7.0.0","sinon":"^22.1.0","in-range":"^3.0.0","time-span":"^5.1.0","p-cancelable":"^4.0.1","@typescript/native":"npm:typescript@^7.0.2"},"_npmOperationalInternal":{"tmp":"tmp/p-timeout_8.0.0_1792066293557_0.3349639356599081","host":"s3://npm-registry-packages-npm-production"},"readme":"# p-timeout\n\n> Timeout a promise after a specified amount of time\n\n> [!NOTE]\n> You may want to use `AbortSignal.timeout()` instead. [Learn more.](#abortsignal)\n\n## Install\n\n```sh\nnpm install p-timeout\n```\n\n## Usage\n\n```js\nimport {setTimeout} from 'node:timers/promises';\nimport pTimeout from 'p-timeout';\n\nconst delayedPromise = setTimeout(200);\n\nawait pTimeout(delayedPromise, {\n\tmilliseconds: 50,\n});\n//=> [TimeoutError: Promise timed out after 50 milliseconds]\n```\n\n## API\n\n### pTimeout(input, options)\n\nReturns a decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout.\n\nIf you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out, and when its `signal` aborts. Cancellation is best-effort: if `.cancel()` throws, the error is ignored and the promise still settles as usual. A promise returned by `.cancel()` is not awaited, so handle its rejection yourself.\n\n#### input\n\nType: `Promise`\n\nPromise to decorate.\n\n#### options\n\nType: `object`\n\n##### milliseconds\n\nType: `number`\n\nMilliseconds before timing out.\n\nPassing `Infinity`, or any value above the maximum delay a timer can represent (`2 ** 31 - 1`), will cause it to never time out.\n\n##### message\n\nType: `string | Error | false`\\\nDefault: `'Promise timed out after {milliseconds} milliseconds'`\n\nSpecify a custom error message or error to throw when it times out:\n\n- `message: 'too slow'` will throw `TimeoutError('too slow')`\n- `message: new MyCustomError('it’s over 9000')` will throw the same error instance\n- `message: false` will make the promise resolve with `undefined` instead of rejecting\n\nIf you do a custom error, it's recommended to sub-class `TimeoutError`:\n\n```js\nimport {TimeoutError} from 'p-timeout';\n\nclass MyCustomError extends TimeoutError {\n\tname = \"MyCustomError\";\n}\n```\n\n##### fallback\n\nType: `Function`\n\nDo something other than rejecting with an error on timeout.\n\nIt takes precedence over `message`, including `message: false`.\n\nThe function can return a value or a promise.\n\nYou could for example retry:\n\n```js\nimport {setTimeout} from 'node:timers/promises';\nimport pTimeout from 'p-timeout';\n\nconst delayedPromise = () => setTimeout(200);\n\nawait pTimeout(delayedPromise(), {\n\tmilliseconds: 50,\n\tfallback: () => {\n\t\treturn pTimeout(delayedPromise(), {\n\t\t\tmilliseconds: 300\n\t\t});\n\t}\n});\n```\n\n##### customTimers\n\nType: `object` with function properties `setTimeout` and `clearTimeout`\n\nCustom implementations for the `setTimeout` and `clearTimeout` functions.\n\nUseful for testing purposes, in particular to work around [`sinon.useFakeTimers()`](https://sinonjs.org/releases/latest/fake-timers/).\n\nExample:\n\n```js\nimport pTimeout from 'p-timeout';\nimport sinon from 'sinon';\n\nconst originalSetTimeout = setTimeout;\nconst originalClearTimeout = clearTimeout;\n\nsinon.useFakeTimers();\n\n// Use `pTimeout` without being affected by `sinon.useFakeTimers()`:\nawait pTimeout(doSomething(), {\n\tmilliseconds: 2000,\n\tcustomTimers: {\n\t\tsetTimeout: originalSetTimeout,\n\t\tclearTimeout: originalClearTimeout\n\t}\n});\n```\n\n##### signal\n\nType: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)\n\nAbort the promise.\n\n```js\nimport pTimeout from 'p-timeout';\nimport delay from 'delay';\n\nconst delayedPromise = delay(3000);\n\nconst abortController = new AbortController();\n\nsetTimeout(() => {\n\tabortController.abort();\n}, 100);\n\nawait pTimeout(delayedPromise, {\n\tmilliseconds: 2000,\n\tsignal: abortController.signal\n});\n```\n\n### TimeoutError\n\nExposed for instance checking and sub-classing.\n\n## Related\n\n- [delay](https://github.com/sindresorhus/delay) - Delay a promise a specified amount of time\n- [p-min-delay](https://github.com/sindresorhus/p-min-delay) - Delay a promise a minimum amount of time\n- [p-retry](https://github.com/sindresorhus/p-retry) - Retry a promise-returning function\n- [More…](https://github.com/sindresorhus/promise-fun)\n\n## AbortSignal\n\n> Modern alternative to `p-timeout`\n\nAsync 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).\n\nThe 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.\n\n```js\n// Call API, timeout after 5 seconds\nconst response = await fetch('./my-api', {signal: AbortSignal.timeout(5000)});\n```\n\n```js\nasync function buildWall(signal) {\n\tfor (const brick of bricks) {\n\t\tsignal.throwIfAborted();\n\t\t// Or: if (signal.aborted) { return; }\n\n\t\tawait layBrick();\n\t}\n}\n\n// Stop long work after 60 seconds\nawait buildWall(AbortSignal.timeout(60_000))\n```\n\nYou 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).\n","publish_time":1792066293651,"_cnpm_publish_time":1792066293651,"dist-tags":{"latest":"8.1.0"}}