Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 402
GreenAsJade Avatar asked Oct 18 '15 04:10

GreenAsJade


1 Answers

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.

like image 90
GreenAsJade Avatar answered Oct 01 '22 19:10

GreenAsJade