mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Create A Cancelable Promise With PCancelable as a javascript til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||||
|
|
||||||
_681 TILs and counting..._
|
_682 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -233,6 +233,7 @@ _681 TILs and counting..._
|
|||||||
- [Check If Something Is An Array](javascript/check-if-something-is-an-array.md)
|
- [Check If Something Is An Array](javascript/check-if-something-is-an-array.md)
|
||||||
- [Computed Property Names In ES6](javascript/computed-property-names-in-es6.md)
|
- [Computed Property Names In ES6](javascript/computed-property-names-in-es6.md)
|
||||||
- [Configure Jest To Run A Test Setup File](javascript/configure-jest-to-run-a-test-setup-file.md)
|
- [Configure Jest To Run A Test Setup File](javascript/configure-jest-to-run-a-test-setup-file.md)
|
||||||
|
- [Create A Cancelable Promise With PCancelable](javascript/create-a-cancelable-promise-with-pcancelable.md)
|
||||||
- [Create An Array Containing 1 To N](javascript/create-an-array-containing-1-to-n.md)
|
- [Create An Array Containing 1 To N](javascript/create-an-array-containing-1-to-n.md)
|
||||||
- [Create An Object With No Properties](javascript/create-an-object-with-no-properties.md)
|
- [Create An Object With No Properties](javascript/create-an-object-with-no-properties.md)
|
||||||
- [Create Bootstrapped Apps With Yarn](javascript/create-bootstrapped-apps-with-yarn.md)
|
- [Create Bootstrapped Apps With Yarn](javascript/create-bootstrapped-apps-with-yarn.md)
|
||||||
|
|||||||
39
javascript/create-a-cancelable-promise-with-pcancelable.md
Normal file
39
javascript/create-a-cancelable-promise-with-pcancelable.md
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
# 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('This promise is being canceled');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
fetchPromise.then(response => {
|
||||||
|
console.log('Promises Resolved: ', response.data);
|
||||||
|
}).catch(err => {
|
||||||
|
console.log('Promises 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.
|
||||||
Reference in New Issue
Block a user