{"name":"p-timeout","version":"6.1.4","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@6.1.4","maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"homepage":"https://github.com/sindresorhus/p-timeout#readme","bugs":{"url":"https://github.com/sindresorhus/p-timeout/issues"},"dist":{"shasum":"418e1f4dd833fa96a2e3f532547dd2abdb08dbc2","tarball":"http://registry-npm.ywwl.com/p-timeout/download/p-timeout-6.1.4.tgz","fileCount":5,"integrity":"sha512-MyIV3ZA/PmyBN/ud8vV9XzwTrNtR4jFrObymZYnZqMmW0zA8Z17vnT0rBgFE/TlohB+YCHqXMgZzb3Csp49vqg==","signatures":[{"sig":"MEQCIFayXH2Yy764trnTsMtLmgWMllOns3yT8dI1XKUKWtMSAiBhGQopt/ZNMLICEOt6J+tQ7PKabxez7Gw/d1Ckxg8G3w==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":13585,"size":4534,"noattachment":false,"key":"/p-timeout/-/p-timeout-6.1.4.tgz"},"type":"module","types":"./index.d.ts","engines":{"node":">=14.16"},"exports":"./index.js","funding":"https://github.com/sponsors/sindresorhus","gitHead":"16b290ed4eed35f977537756411261f476623586","scripts":{"test":"xo && ava && tsd"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"repository":{"url":"git+https://github.com/sindresorhus/p-timeout.git","type":"git"},"_npmVersion":"10.9.0","description":"Timeout a promise after a specified amount of time","directories":{},"sideEffects":false,"_nodeVersion":"23.3.0","_hasShrinkwrap":false,"devDependencies":{"xo":"^0.54.2","ava":"^4.3.1","tsd":"^0.22.0","delay":"^5.0.0","sinon":"^19.0.2","in-range":"^3.0.0","time-span":"^5.1.0","p-cancelable":"^4.0.1"},"_npmOperationalInternal":{"tmp":"tmp/p-timeout_6.1.4_1735736825200_0.8845203635517245","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.\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` 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\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":1735736825386,"_cnpm_publish_time":1735736825386,"dist-tags":{"latest":"8.1.0"}}