Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Commit

Permalink
use case #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Gruenbaum committed May 25, 2018
1 parent c84b27c commit 38f756f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion meeting-discussion.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Methodology:
- Unhandled Rejections:
- Our current heuristic can be problematic with async functions (use case #8)
- No throw on GC yet (Reuben is working on it) (use case #9)
- People don't trust automatic unhandled rejection detection and add `.catch(error => console.log('error'))`
- People don't trust automatic unhandled rejection detection and add `.catch(error => console.log('error'))` (use case #10)

- Forgotten promises:
- If I have a promise that never resolves - there is no easy way to know about it.
Expand Down
33 changes: 33 additions & 0 deletions use-cases/10/10.md
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.
34 changes: 34 additions & 0 deletions use-cases/10/code.js
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)
*/

0 comments on commit 38f756f

Please sign in to comment.