All Projects → wojtekmaj → make-cancellable-promise

wojtekmaj / make-cancellable-promise

Licence: MIT license
Make any Promise cancellable.

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to make-cancellable-promise

PromisedFuture
A Swift based Future/Promises framework to help writing asynchronous code in an elegant way
Stars: ✭ 75 (+341.18%)
Mutual labels:  promise
best-queue
Queue in runtime based promise
Stars: ✭ 26 (+52.94%)
Mutual labels:  promise
Promise.allSettled
ES Proposal spec-compliant shim for Promise.allSettled
Stars: ✭ 93 (+447.06%)
Mutual labels:  promise
PiedPiper
A small set of classes and functions to make easy use of Futures, Promises and async computation in general. All written in Swift for iOS 10+, WatchOS 3, tvOS and Mac OS X apps.
Stars: ✭ 44 (+158.82%)
Mutual labels:  promise
relaks
Asynchrounous React component
Stars: ✭ 49 (+188.24%)
Mutual labels:  promise
sequence-as-promise
Executes array of functions as sequence and returns promise
Stars: ✭ 23 (+35.29%)
Mutual labels:  promise
wtsqs
Simplified Node AWS SQS Worker Wrapper
Stars: ✭ 18 (+5.88%)
Mutual labels:  promise
parcel-plugin-goodie-bag
provides the Promise and fetch goodies needed for IE(11) support w/ parcel bundle loading
Stars: ✭ 15 (-11.76%)
Mutual labels:  promise
eslint-config-welly
😎 ⚙️ ESLint configuration for React projects that I do. Feel free to use this!
Stars: ✭ 21 (+23.53%)
Mutual labels:  promise
hangman-game
A responsive hangman game built with vanilla javascript, html, and css. Animated with GSAP and canvas animations.
Stars: ✭ 22 (+29.41%)
Mutual labels:  promise
promise-abortable
Promise lib for aborting in chain.
Stars: ✭ 19 (+11.76%)
Mutual labels:  promise
repromised
🤝 Declarative promise resolver as a render props component
Stars: ✭ 20 (+17.65%)
Mutual labels:  promise
lightflow
A tiny Promise-inspired control flow library for browser and Node.js.
Stars: ✭ 29 (+70.59%)
Mutual labels:  promise
nodeJS examples
Server, routing, db examples using NodeJS v6
Stars: ✭ 34 (+100%)
Mutual labels:  promise
iworker
Promise-based wrapper for worker_threads
Stars: ✭ 18 (+5.88%)
Mutual labels:  promise
co demo
A step-by-step guide about how to avoid callback hell with ES6 Promises + generators (aka make your own "co")
Stars: ✭ 17 (+0%)
Mutual labels:  promise
p-cache
Decorator to memoize the results of async functions via lru-cache.
Stars: ✭ 21 (+23.53%)
Mutual labels:  promise
emacs-promise
Promises/A+ for Emacs
Stars: ✭ 56 (+229.41%)
Mutual labels:  promise
conquerant
lightweight async/await for Clojure
Stars: ✭ 31 (+82.35%)
Mutual labels:  promise
transceiver
Channel based event bus with request/reply pattern, using promises. For node & browser.
Stars: ✭ 25 (+47.06%)
Mutual labels:  promise

npm downloads CI tested with jest

Make-Cancellable-Promise

Make any Promise cancellable.

tl;dr

  • Install by executing npm install make-cancellable-promise or yarn add make-cancellable-promise.
  • Import by adding import makeCancellablePromise from 'make-cancellable-promise.
  • Do stuff with it!
    const { promise, cancel } = makeCancellablePromise(myPromise);

User guide

makeCancellablePromise(myPromise)

A function that returns an object with two properties:

promise and cancel. promise is a wrapped around your promise. cancel is a function which stops .then() and .catch() from working on promise, even if promise passed to makeCancellablePromise resolves or rejects.

Usage

const { promise, cancel } = makeCancellablePromise(myPromise);

Typically, you'd want to use makeCancellablePromise in React components. If you call setState on an unmounted component, React will throw an error.

Here's how you can use makeCancellablePromise with React:

function MyComponent() {
  const [status, setStatus] = useState('initial');

  useEffect(() => {
    const { promise, cancel } = makeCancellable(fetchData());

    promise.then(() => setStatus('success')).catch(() => setStatus('error'));

    return () => {
      cancel();
    };
  }, []);

  const text = (() => {
    switch (status) {
      case 'pending':
        return 'Fetching…';
      case 'success':
        return 'Success';
      case 'error':
        return 'Error!';
      default:
        return 'Click to fetch';
    }
  })();

  return <p>{text}</p>;
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].