# @byungi/p-cancel

> A cancelable promise.

Latest version **0.2.1** (published 2019-12-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @byungi/p-cancel
pnpm add @byungi/p-cancel
yarn add @byungi/p-cancel
bun add @byungi/p-cancel
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2019-12-30 |
| First published | 2019-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 8 |
| Dependencies | 2 |
| Unpacked size | 5.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | skt-t1-byungi |
| Maintainers | skt-t1-byungi |
| Keywords | promise, cancel, cancelable, cancellable, cancellation, abort |

## Links

- npm: https://www.npmjs.com/package/@byungi/p-cancel
- Repository: https://github.com/skt-t1-byungi/promise/tree/master/packages/p-cancel
- npm.io page: https://npm.io/package/@byungi/p-cancel

## Dependencies (2)

- [@byungi/p-class](https://npm.io/package/@byungi/p-class.md) ^0.2.0
- [@byungi/promise-helpers](https://npm.io/package/@byungi/promise-helpers.md) ^0.2.0

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 0.2.1 (latest) — 2019-12-30
- 0.2.0 — 2019-12-28
- 0.1.3 — 2019-09-21
- 0.1.2 — 2019-03-10
- 0.1.1 — 2019-03-09
- 0.1.0 — 2019-03-09
- 0.0.3 — 2019-01-21
- 0.0.2 — 2019-01-21
- 0.0.1 — 2019-01-15

## README

# @byungi/p-cancel
A cancelable promise.

## Example
```js
import PCancel, {CancelError} from '@byungi/p-cancel'

const requestPromise = new PCancel((resolve, reject, onCancel) => {
    var xhr = new XMLHttpRequest();
    xhr.on('load', resolve);
    xhr.on('error', reject);
    xhr.open('GET', 'http://delay/500ms/call', true);
    xhr.send(null);

    onCancel(()=> xhr.abort())
})

console.log(requestPromise.isCanceled) // => false

setTimeout(()=> {
    requestPromise.cancel() // => After 100ms, xhr is aborted.
}, 100)

requestPromise.catch(err => {
    console.log(err.isCanceled) // => true
    console.log(err instanceof CancelError) // => true
    console.log(requestPromise.isCanceled) // => true
})
```

## API
### new PCancel(executor)
`PCancel` is a promise implementation. Same as promise creation except for `onCancel`. `onCancel` receives a function to operate on `cancel()`.

```js
const promise = new PCancel((resolve, reject, onCancel) => {
    const timerId = setTimeout(lazyJob, 1000)
    onCancel(()=> clearTimeout(timerId))
})
```

### promise.cancel([reason])
Execute the cancel operation added with `onCancel` and throw a `CancelError`.

### promise.isCanceled
Returns whether the promise is canceled.

### promise.pipe(onFulfilled, onRejected)
Similar to `then` but can propagate `cancel` to the upper promise.

```js
const handleWithRequestPromise = requestPromise.pipe(response => {
    handleResponse(response)
})

handleWithRequestPromise.cancel() // => xhr is aborted
```

## License
MIT

---
_Source: https://npm.io/package/@byungi/p-cancel · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
