Git Product home page Git Product logo

Comments (3)

jopemachine avatar jopemachine commented on July 18, 2024 1

I think onResolve and onReject function needs to be updated like below to prevent state change after canceling.

const onReject = error => {
  if (this.#state !== promiseState.canceled || !onCancel.shouldReject) {
    reject(error);

    if (this.#state !== promiseState.canceled) {
      this.#state = promiseState.rejected;
    }
  }
};

Then, the example code result will be like the below (expected result)

canceled
Second main catch error: Error: canceled
Don't worry it was canceled

I just made a PR fixing it in this way.

from p-cancelable.

Rychu-Pawel avatar Rychu-Pawel commented on July 18, 2024

Ok, I see that it is because there is a reject(new Error('canceled')); line in the onCancel handler which overwrites the state of the promise. But actually, I find it a bit confusing that the promise reports it was not canceled when it actually was.

Maybe the promise should additionally rely on a new internal variable isCanceled rather than only on the state which looks like is overwritable?

from p-cancelable.

sindresorhus avatar sindresorhus commented on July 18, 2024

This looks like a bug. A canceled state should be immutable.

@jopemachine Thoughts?

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.