This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Benjamin Gruenbaum
committed
May 25, 2018
1 parent
c84b27c
commit 38f756f
Showing
3 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
### Use Case #10 | ||
|
||
Name: People don't trust automatic unhandled rejection detection and add `.catch(error => console.log('error'))` | ||
|
||
Kyle is writing code using promises cahining and/or `async/await`. He adds catch handlers where he logs the errors: | ||
|
||
|
||
### What happens | ||
|
||
Currently, they are writing code like: | ||
|
||
```js | ||
async function foo() { | ||
try { | ||
await someAsyncFn(); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
Or: | ||
|
||
```js | ||
someAsyncFn().catch(e => console.error(e)); | ||
``` | ||
|
||
### Why it happens | ||
|
||
Because we haven't gained a level of trust from the community for our default debugging tools. Because people don't know that we log that or because we've only been logging the full error pretty recently. | ||
### What can we maybe do better | ||
Provide a better experience with the `unhandledRejection` and error hooks or communicate that users do not need to add these handlers. In addition they also can mess up the flow control if they are not very used. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const delay = require('util').promisify(setTimeout); | ||
|
||
const someAsyncFn = async () => await b(); | ||
const b = async () => { | ||
await delay(10); | ||
await c(); | ||
} | ||
const c = async () => { | ||
await delay(10); | ||
await d(); | ||
} | ||
const d = async () => { throw new Error('Error!'); }; | ||
|
||
|
||
someAsyncFn(); | ||
|
||
/** | ||
* logs with the default handler: | ||
* ~/Documents/OpenSource/promise-use-cases/use-cases/10 [master] $ ../../../node/out/Release/node code.js | ||
(node:32995) UnhandledPromiseRejectionWarning: Error: Error! | ||
at d (/Users/benjamin/Documents/OpenSource/promise-use-cases/use-cases/10/code.js:12:31) | ||
at c (/Users/benjamin/Documents/OpenSource/promise-use-cases/use-cases/10/code.js:10:9) | ||
(node:32995) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) | ||
(node:32995) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. | ||
*/ | ||
|
||
|
||
// with .catch(e => console.error(e)); | ||
/** | ||
* ~/Documents/OpenSource/promise-use-cases/use-cases/10 [master] $ ../../../node/out/Release/node code.js | ||
Error: Error! | ||
at d (/Users/benjamin/Documents/OpenSource/promise-use-cases/use-cases/10/code.js:12:31) | ||
at c (/Users/benjamin/Documents/OpenSource/promise-use-cases/use-cases/10/code.js:10:9) | ||
*/ |