{"_id":"p-cancelable@5.0.0","bugs":{"url":"https://github.com/sindresorhus/p-cancelable/issues"},"dist":{"shasum":"7079af119b5b47ab80d7ddf0632bb31f7c9f6aaa","tarball":"http://registry-npm.ywwl.com/p-cancelable/download/p-cancelable-5.0.0.tgz","fileCount":5,"integrity":"sha512-iElBCXM5F6sGrAvarF/uYLdtEe4KgOUuzgePUUEMwgDsRdwiQh9KxEHO2bz6kURwUtKqhrHNffR4s8Qfl5j4ZQ==","signatures":[{"sig":"MEUCIH1ksXhT7zA8Z3ekNYZr8xYyipF0h06M0gUgh6nZlHdgAiEAy3NFzjHbhpV5CikZSGdt0FFpFH8a3bMshoXcOPoUgmo=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIAK70nyK92uMqY9TfMumgtvrXhLJ9Vwgv6k04F76DZnIAiAGNUmBJZY2K5d9dzRbh2opFhxwZ5yoCAKOpbUlqE865g=="}],"unpackedSize":15830,"size":5099,"noattachment":false,"key":"/p-cancelable/-/p-cancelable-5.0.0.tgz"},"name":"p-cancelable","type":"module","types":"./index.d.ts","author":{"url":"https://sindresorhus.com","name":"Sindre Sorhus","email":"sindresorhus@gmail.com"},"engines":{"node":">=22"},"exports":{"types":"./index.d.ts","default":"./index.js"},"funding":"https://github.com/sponsors/sindresorhus","gitHead":"e5c8f4000acd7a3a46f91ddcf58a90037e98fd9e","license":"MIT","scripts":{"test":"xo && node --test && nz"},"version":"5.0.0","_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"homepage":"https://github.com/sindresorhus/p-cancelable#readme","keywords":["promise","cancelable","cancel","canceled","canceling","cancellable","cancellation","abort","abortable","aborting","cleanup","task","token","async","function","await","promises","bluebird"],"repository":{"url":"git+https://github.com/sindresorhus/p-cancelable.git","type":"git"},"_npmVersion":"12.2.0","description":"Create a promise that can be canceled","directories":{},"maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"sideEffects":false,"_nodeVersion":"26.12.1","_hasShrinkwrap":false,"devDependencies":{"nz":"^0.1.0","xo":"^5.0.1","delay":"^7.0.0","@typescript/native":"npm:typescript@^7.0.2"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/p-cancelable_5.0.0_1793649824683_0.021357956252746946"},"readme":"# p-cancelable\n\n> Create a promise that can be canceled\n\nUseful for animation, loading resources, long-running async computations, async iteration, etc.\n\n**This package is in maintenace state. No new features will be added.**\n\n*If you target [Node.js 16](https://medium.com/@nodejs/node-js-v15-0-0-is-here-deb00750f278) or later, this package is [less useful](https://github.com/sindresorhus/p-cancelable/issues/27) and you should probably use [`AbortController`](https://developer.mozilla.org/docs/Web/API/AbortController) instead.*\n\n## Install\n\n```sh\nnpm install p-cancelable\n```\n\n## Usage\n\n```js\nimport PCancelable from 'p-cancelable';\n\nconst cancelablePromise = new PCancelable((resolve, reject, onCancel) => {\n\tconst worker = new SomeLongRunningOperation();\n\n\tonCancel(() => {\n\t\tworker.close();\n\t});\n\n\tworker.on('finish', resolve);\n\tworker.on('error', reject);\n});\n\n// Cancel the operation after 10 seconds\nsetTimeout(() => {\n\tcancelablePromise.cancel('Unicorn has changed its color');\n}, 10000);\n\ntry {\n\tconsole.log('Operation finished successfully:', await cancelablePromise);\n} catch (error) {\n\tif (cancelablePromise.isCanceled) {\n\t\t// Handle the cancelation here\n\t\tconsole.log('Operation was canceled');\n\t\treturn;\n\t}\n\n\tthrow error;\n}\n```\n\n## API\n\n### new PCancelable(executor)\n\nSame as the [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise), but with an appended `onCancel` parameter in `executor`.\n\nCancelling will reject the promise with `CancelError`. To avoid that, set `onCancel.shouldReject` to `false`.\n\n```js\nimport PCancelable from 'p-cancelable';\n\nconst cancelablePromise = new PCancelable((resolve, reject, onCancel) => {\n\tconst job = new Job();\n\n\tonCancel.shouldReject = false;\n\tonCancel(() => {\n\t\tjob.stop();\n\t});\n\n\tjob.on('finish', resolve);\n});\n\ncancelablePromise.cancel(); // Doesn't throw an error\n```\n\n`PCancelable` is a subclass of `Promise`.\n\n#### onCanceled(fn)\n\nType: `Function`\n\nAccepts a function that is called when the promise is canceled.\n\nYou're not required to call this function. You can call this function multiple times to add multiple cancel handlers.\n\nIf a cancel handler throws, the other handlers still run, and the promise is rejected with the first error.\n\n### PCancelable#cancel(reason?)\n\nType: `Function`\n\nCancel the promise and optionally provide a reason.\n\nThe cancellation is synchronous. Calling it after the promise has settled or multiple times does nothing.\n\n### PCancelable#isCanceled\n\nType: `boolean`\n\nWhether the promise is canceled.\n\n### PCancelable.fn(fn)\n\nConvenience method to make your promise-returning or async function cancelable.\n\nThe function you specify will have `onCancel` appended to its parameters.\n\n```js\nimport PCancelable from 'p-cancelable';\n\nconst fn = PCancelable.fn((input, onCancel) => {\n\tconst job = new Job();\n\n\tonCancel(() => {\n\t\tjob.cleanup();\n\t});\n\n\treturn job.start(); //=> Promise\n});\n\nconst cancelablePromise = fn('input'); //=> PCancelable\n\n// …\n\ncancelablePromise.cancel();\n```\n\n### CancelError\n\nType: `Error`\n\nRejection reason when `.cancel()` is called.\n\nIt includes a `.isCanceled` property for convenience.\n\n## FAQ\n\n### Cancelable vs. Cancellable\n\n[In American English, the verb cancel is usually inflected canceled and canceling—with one l.](http://grammarist.com/spelling/cancel/) Both a [browser API](https://developer.mozilla.org/en-US/docs/Web/API/Event/cancelable) and the [Cancelable Promises proposal](https://github.com/tc39/proposal-cancelable-promises) use this spelling.\n\n### What about the official [Cancelable Promises proposal](https://github.com/tc39/proposal-cancelable-promises)?\n\n~~It's still an early draft and I don't really like its current direction. It complicates everything and will require deep changes in the ecosystem to adapt to it. And the way you have to use cancel tokens is verbose and convoluted. I much prefer the more pragmatic and less invasive approach in this module.~~ The proposal was withdrawn.\n\n## Related\n\n- [p-progress](https://github.com/sindresorhus/p-progress) - Create a promise that reports progress\n- [p-lazy](https://github.com/sindresorhus/p-lazy) - Create a lazy promise that defers execution until `.then()` or `.catch()` is called\n- [p-signal](https://github.com/astoilkov/p-signal) - Cancel promises using `AbortSignal`\n- [More…](https://github.com/sindresorhus/promise-fun)\n","publish_time":1793649824770,"_cnpm_publish_time":1793649824770,"dist-tags":{"latest":"5.0.0"}}