@m9ch/make-cancelable-promise
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

@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

Dependencies (0)

    Dev Dependencies (7)

    Package Sidebar

    Install

    npm i @m9ch/make-cancelable-promise

    Weekly Downloads

    1

    Version

    0.1.0

    License

    MIT

    Unpacked Size

    6.32 kB

    Total Files

    6

    Last publish

    Collaborators

    • mitscherlich36