Why do we have to call `.done()` at the end of a p

2020-08-26 10:48发布

问题:

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?

回答1:

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.