Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide .done() or other method to throw Exception out of chain #42

Closed
ivan-kleshnin opened this issue Feb 12, 2015 · 2 comments
Closed

Comments

@ivan-kleshnin
Copy link

Consider this code:

// fragment of react-based app

Axios.get('/api/robots/')
  .then((res) => { // could be .done() if... see below
    let self = this;
    //setTimeout(function() { // -- this is ugly disabled workaround --
      self.setState(res.data); // if error happen here
    //}, 1);
    console.log(">>> RobotStore.state:", this.state);
  })
  .catch((res) => {
    if (res instanceof Error) { // it is catched here
      // unpredictable error, release 
      throw Error("test"); // this DOES NOT work, error is swallowed (reject promise which is never used)
    } else {
      // HTTP error, handle
      this.setState(this.getInitialState());
    }
  }); 

So axios (actually ES6 Promise engine) swallows app errors (which I want to bubble up and crash everything, as it's not runtime error).

http://bahmutov.calepin.co/why-promises-need-to-be-done.html

But looks like done still is not a part of spec and "ugly workaround" is required. Some authors insist on deprecating done at all.

http://stackoverflow.com/questions/26667598/will-javascript-es6-promise-support-done-api

I'ml aware of all aspects of this subject. What can you propose to solve this issue?
Monkeypatch .done? Leave setTimeout "trick" as it is? Or there are other ways?

@mzabriskie
Copy link
Member

I am really frustrated with ES6 Promises. I was unfortunately unaware of the error swallowing behavior when I first started with axios, and thought that building on standardized, native Promises was the right thing. I am questioning that decision now.

I have thought a lot about what should be done, and have ultimately concluded that axios itself won't do anything to try and solve the problem. Instead I will let consumers decide how they want to handle it. Long term, browser dev tools should report an error when the Promise is garbage collected.

Until then I personally would consider using a polyfill for done, such as https://www.promisejs.org/polyfills/promise-done-6.1.0.js. I think that it's cleaner than doing the setTimeout yourself.

@ivan-kleshnin
Copy link
Author

@mzabriskie, thank you for this comprehensive answer!
I actually agree with your advice, and already made the same choice of polyfilling.

@axios axios locked and limited conversation to collaborators May 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants