0.1.0 • Published 2 years ago

@m9ch/make-cancelable-promise v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@m9ch/make-cancelable-promise

npm version npm downloads

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