https://github.com/mitscherlich/make-cancelable-promise
Make any promise cancelable with AbortController & AbortSignal
https://github.com/mitscherlich/make-cancelable-promise
Last synced: about 1 year ago
JSON representation
Make any promise cancelable with AbortController & AbortSignal
- Host: GitHub
- URL: https://github.com/mitscherlich/make-cancelable-promise
- Owner: Mitscherlich
- License: mit
- Created: 2022-05-18T16:11:21.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-18T16:56:54.000Z (about 4 years ago)
- Last Synced: 2025-06-27T22:04:32.207Z (about 1 year ago)
- Language: TypeScript
- Homepage: https://npm.im/@m9ch/make-cancelable-promise
- Size: 38.1 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @m9ch/make-cancelable-promise
[](https://npm.im/@m9ch/make-cancelable-promise) [](https://npm.im/@m9ch/make-cancelable-promise)
Make any promise cancelable.
## Install
via `pnpm`, `yarn` or `npm`:
```bash
pnpm add @m9ch/make-cancelable-promise
# or
yarn add @m9ch/make-cancelable-promise
# or
npm i -S @m9ch/make-cancelable-promise
```
## Usage
```javascript
import { makeCancelablePromise } from '@m9ch/make-cancelable-promise'
const abort = new AbortController()
const cancelable = makeCancelablePromise(
new Promise((resolve, reject) => {
setTimeout(() => {
resolve('done')
}, 1000)
}),
abort.signal
)
setTimeout(() => {
cancelable.cancel('The operation was aborted')
}, 500)
// or with abort signal
abort.abort()
cancelable.promise
.then(console.log) // => undefined
.catch(console.error) // => AbortError: The operation was aborted
```
## License
MIT © [Mitscherlich](https://mitscherlich.me)