Git Product home page Git Product logo

Comments (8)

slorber avatar slorber commented on July 18, 2024 2

@bisubus no particular problem, totally unrelated to inheritance. It's just I needed cancellation for certain patterns related, but not strictly coupled to React (like handling race conditions etc).

I've made my own lib for that: https://github.com/slorber/awesome-imperative-promise

from p-cancelable.

slorber avatar slorber commented on July 18, 2024 1

@sindresorhus I finally was able to find back this React blog post explaining what I want to do: https://reactjs.org/blog/2015/12/16/ismounted-antipattern.html

Also, note that this lib does not work with React Native, only in prod mode. I got errors, as far as I remember it was due to Object.setPrototypeOf(PCancelable.prototype, Promise.prototype);

I'm going to use the solution in the blog post instead, just wanted to let you know

from p-cancelable.

sindresorhus avatar sindresorhus commented on July 18, 2024

Promises are eager, so it has already started by the time you pass it to PCancelable.fromPromise. That's why it must be wrapped in a function.

Why are you even using this module if you don't intend to handle the cancelation? :)

from p-cancelable.

slorber avatar slorber commented on July 18, 2024

from p-cancelable.

sindresorhus avatar sindresorhus commented on July 18, 2024

I'm using it mostly to disable then callbacks

What do you mean by that? Example?

from p-cancelable.

slorber avatar slorber commented on July 18, 2024

from p-cancelable.

fritx avatar fritx commented on July 18, 2024

I'm using it mostly to disable then callbacks, not to tear down resources.

Btw, there are also cases we could not only disable then callbacks but also tear down resources.

For example, ajax, we could call xhr.abort() before the server responds when canceling a Promise.

from p-cancelable.

bisubus avatar bisubus commented on July 18, 2024

@sindresorhus Can we reopen the issue? It's legit IMO. If you need to wrap existing promise, you really do.

My primary case for cancelable promises are promise-based APIs like HTTP requests. PCancelable alone would feel like promise constructor antipattern to me here:

const fromPromise = (promise, onCancel) => (
	new PCancelable((resolve, reject, addCancelHandler) => {
		addCancelHandler(onCancel);
		promise.then(resolve, reject);
	});
);

fromPromise(
    fetch(url, { signal: controller.signal }),
    () => controller.abort()
);

Notice that executor param name in documentation is misleading; on implies that it's a handler, while it's a function that registers a handler. Could be addCancelHandler or so.

@slorber Can you elaborate what was the problem with React in your case? I believe I had issues with Promise inheritance before.

from p-cancelable.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.