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?
-
1
"Unhandled exceptions in a
then
function are silently captured as part of the state of the promise, but unhandled exceptions in adone
function are thrown." If you don't do adone
, 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, whereasthen
saves the exception in the promise so it can propagate it into the nextthen
call. The problem is that if there is no nextthen
, then the exception never gets propagated to anywhere; it just vanishes. The upshot is that an emptydone
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
1 Answer
Reset to default 5What I needed clarified:
- Exceptions encountered in promises (during execution of the
then()
callback) are stored as anError
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.