admin管理员组文章数量:1431272
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.
本文标签: javascriptWhy do we have to call done() at the end of a promise chain in reactnativeStack Overflow
版权声明:本文标题:javascript - Why do we have to call `.done()` at the end of a promise chain in react-native? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745570767a2664033.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论