Facebook discusses this on its blog: https://reactjs.org/blog/2015/12/16/ismounted-antipattern.ht...
I've been using Observables which, in my experience, bind a little cleaner than Promises to React components.
Facebook discusses this on its blog: https://reactjs.org/blog/2015/12/16/ismounted-antipattern.ht...
I've been using Observables which, in my experience, bind a little cleaner than Promises to React components.
I've thought it's just technically impossible (no API to do so) to cancel a `fetch` request in progress.
Native promises do not have a "real" cancellation API (no 3rd path besides resolved and rejected) but you can mimic the functionality by rejecting with a dedicated error type, or use another promise as a cancellation token for example.
fetch(), which uses promises as return type, do not support aborting requests, as discussed [here](https://github.com/whatwg/fetch/issues/447) but the [old XMLHttpRequest does](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequ...).
Aborting is specific to HTTP requests, it's stopping the connection to the remote server (so technically impossible to implement with the current APIs as you said). Cancelling a promise is just stopping all onResolved continuations to be called, so it can be implemented in several different ways as I explained above.
The end result if you use a classic cancellation wrapper on a fetch() promise is that the onReject continuations will be called immediately with a specific reason. The HTTP request will continue to the end (no abort), but the response will be ignored.
In the case of the react library I mentioned, both the onResolved and onReject continuations will be ignored if cancelled, it's the subtlety that allows the component to be garbage collected (and probably why the author decided to call that "thrashable" as opposed to "cancellable" to mark the difference).
It's not about cancelling any potential inflight action within the promise itself.
In this case, you don't want the callback to run after the react component unmounts, nor do you want to hold a reference to the component:
promise
.then(value => this.setState({ value }))But I would never do something like that in, say, the constructor of a WPF/UWP windows app as those have "Loaded" or "Loading" events of some kind (which is parallel to componentDidMount).
The React lifecycle has `componentDidMount` for doing stuff (at most once) after rendering.
Once the component mounts, then it can be unmounted. The upstream comment is talking about when you have a promise that is still running after unmount.
componentDidMount() {
if (!this.unmounted) ...
}
componentWillUnmount() {
this.unmounted = true
} this.cancelOnUnmoun(new Promise(...)).then(...)