From 70050e24a9ab0c5250f65d50535555b29b31a75b Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 31 May 2018 09:52:35 -0500 Subject: [PATCH] Add Create A Cancelable Promise With PCancelable as a javascript til --- README.md | 3 +- ...e-a-cancelable-promise-with-pcancelable.md | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 javascript/create-a-cancelable-promise-with-pcancelable.md diff --git a/README.md b/README.md index f22db9c..0eecc4b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [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) - [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) +- [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 Object With No Properties](javascript/create-an-object-with-no-properties.md) - [Create Bootstrapped Apps With Yarn](javascript/create-bootstrapped-apps-with-yarn.md) diff --git a/javascript/create-a-cancelable-promise-with-pcancelable.md b/javascript/create-a-cancelable-promise-with-pcancelable.md new file mode 100644 index 0000000..40da420 --- /dev/null +++ b/javascript/create-a-cancelable-promise-with-pcancelable.md @@ -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.