# Create A Cancelable Promise With PCancelable

If an async task takes a really long time and we find out in the middle of its execution that we no longer want the results of that activity, it would be nice to be able to cancel that promise. There was a [proposal for cancelable promises](https://github.com/tc39/proposal-cancelable-promises), but it has since been withdrawn. There is an alternative though.

The [`p-cancelable`](https://github.com/sindresorhus/p-cancelable) package provides a promise wrapper that acts as a cancelable promise.

```javascript
import PCancelable from 'p-cancelable';

const fetchPromise = new PCancelable((resolve, reject, onCancel) => {
  setTimeout(() => {
    resolve({ ok: true, data: [1, 2, 3] });
  }, 10000);

  onCancel(() => {
    console.log('Promise is being canceled');
  });
});

fetchPromise.then(response => {
  console.log('Promise Resolved: ', response.data);
}).catch(err => {
  console.log('Promise Rejected: ', err);
});

fetchPromise.cancel();
```

We can create a promise in a familiar manner. We get an additional argument -- the `onCancel` function. This is a function that will be executed if `cancel` is called before the promises has resolved or rejected. In the above example, the `fetchPromise.cancel()` line will be invoked before the `setTimeout` resolves. The promise will be canceled, causing a rejection which will push us into the `catch` handler.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/javascript/create-a-cancelable-promise-with-pcancelable.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
