@m9ch/make-cancelable-promise
v0.1.0
Published
Make any promise cancelable.
Downloads
5
Readme
@m9ch/make-cancelable-promise
Make any promise cancelable.
Install
via pnpm
, yarn
or npm
:
pnpm add @m9ch/make-cancelable-promise
# or
yarn add @m9ch/make-cancelable-promise
# or
npm i -S @m9ch/make-cancelable-promise
Usage
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