最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why do we have to call `.done()` at the end of a promise chain in react-native? - Stack Overflow

programmeradmin0浏览0评论

In the react-native tutorial it says:

Note that we call done() at the end of the promise chain - always make sure to call done() or any errors thrown will get swallowed.

 fetchData: function() {
    fetch(REQUEST_URL)
      .then((response) => response.json())
      .then((responseData) => {
        this.setState({
          movies: responseData.movies,
        });
      })
      .done();
  },

What does this empty .done() actually do?

In the react-native tutorial it says:

Note that we call done() at the end of the promise chain - always make sure to call done() or any errors thrown will get swallowed.

 fetchData: function() {
    fetch(REQUEST_URL)
      .then((response) => response.json())
      .then((responseData) => {
        this.setState({
          movies: responseData.movies,
        });
      })
      .done();
  },

What does this empty .done() actually do?

Share Improve this question asked Oct 18, 2015 at 4:52 GreenAsJadeGreenAsJade 14.7k11 gold badges67 silver badges103 bronze badges 5
  • 1 "Unhandled exceptions in a then function are silently captured as part of the state of the promise, but unhandled exceptions in a done function are thrown." If you don't do a done, then any error that occurs is stored inside the promise, and then you throw the promise away and you never learn about the error. – Raymond Chen Commented Oct 18, 2015 at 5:50
  • I imagine this is the beginning of understanding what is happening, but what does it mean that the done is not taking an argument? What does the done actually do? – GreenAsJade Commented Oct 18, 2015 at 5:54
  • The documentation explains what happens when you don't pass a value. (Basically, nothing happens.) The point is that done raises the exception if nobody handles it, whereas then saves the exception in the promise so it can propagate it into the next then call. The problem is that if there is no next then, then the exception never gets propagated to anywhere; it just vanishes. The upshot is that an empty done means "Raise any pending exception now." – Raymond Chen Commented Oct 18, 2015 at 6:43
  • 1 @RaymondChen you should transform your ments into answer – Yevgen Safronov Commented Oct 18, 2015 at 8:10
  • @EugeneSafronov I had originally hoped that a nudge would be sufficient. Didn't intend it to turn into an answer. – Raymond Chen Commented Oct 18, 2015 at 15:49
Add a ment  | 

1 Answer 1

Reset to default 5

What I needed clarified:

  • Exceptions encountered in promises (during execution of the then() callback) are stored as an Error object, and not thrown.

This mechanism means that you can defer actions without risk of exceptions inside them messing you up at a random time.

  • done() called without argument on a promise looks into the promise to see if there are any stored exceptions, and throws them.

This means that you can take care of exceptions during promise processing, at the end of the promise processing.

发布评论

评论列表(0)

  1. 暂无评论