Update blog post which creates unhandled promise rejection (#9668)
* Update 2015-12-16-ismounted-antipattern.md In case anybody else stumbles across this old blog post, I wanted to submit a patch to help with unhandled rejections. `#then` and `#catch` each return new Promise instances, so here we actually create two new promises (that aren't assigned). If the argument promise to `#makeCancelable` rejects, the promise created by `#then` will be an unhandled rejection, which in Node 7 will be an uncaught error. By using the second argument of `#then` to handle rejections instead, we don't need to worry about the runtime finding any unhandled rejections here. * Style updates * Add update notice
Will Myers committed
May 26, 2017 at 09:35 UTC
546e7721ec8ea2eda36706b7b4905901b45b35bc
1 file changed
+6
-6
docs/_posts/2015-12-16-ismounted-antipattern.md
+6
-6
@@ -54,18 +54,16 @@ cancelablePromise
54
cancelablePromise.cancel(); // Cancel the promise
55
```
56
57
-Where `makeCancelable` is [defined by @istarkov](https://github.com/facebook/react/issues/5465#issuecomment-157888325) as:
57
+Where `makeCancelable` was originally [defined by @istarkov](https://github.com/facebook/react/issues/5465#issuecomment-157888325) as:
58
59
```js
60
const makeCancelable = (promise) => {
61
let hasCanceled_ = false;
62
63
const wrappedPromise = new Promise((resolve, reject) => {
64
- promise.then((val) =>
65
- hasCanceled_ ? reject({isCanceled: true}) : resolve(val)
66
- );
67
- promise.catch((error) =>
68
- hasCanceled_ ? reject({isCanceled: true}) : reject(error)
64
+ promise.then(
65
+ val => hasCanceled_ ? reject({isCanceled: true}) : resolve(val),
66
+ error => hasCanceled_ ? reject({isCanceled: true}) : reject(error)
67
);
68
});
69
@@ -78,3 +76,5 @@ const makeCancelable = (promise) => {
76
};
77
```
78
As an added bonus for getting your code cleaned up early, getting rid of `isMounted()` makes it one step easier for you to upgrade to ES6 classes, where using `isMounted()` is already prohibited. Happy coding!
79
+
80
+* _Update 2017-05-12: altered `#makeCancelable` implementation so rejected promises won't go uncaught._