You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+19-17Lines changed: 19 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -11,14 +11,13 @@ really simple error handling with await/async
11
11
inspired by [`await-to-js`](https://github.com/scopsy/await-to-js) whose creator [Dima Grossman](http://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/) originally blogged about using destructuring array assignment
12
12
13
13
## Overview
14
-
This package basically provides 3 syntactical paths to choose from:
14
+
This package basically provides 2 main syntaxes to choose from:
Using the decorator pattern with `handler` yields some cleaner high level code:
51
-
52
-
```javascript
53
-
const {handler} =require('await-on');
54
-
letfetchData= () =>newPromise(/*...*/);
55
-
fetchData =handler(fetchData);
56
-
57
-
asyncfunctionfoo(req,res) {
58
-
const [err, data] =awaitfetchData();
59
-
!err ?res.send(data) :res.send(err);
60
-
}
61
-
```
62
-
63
-
Finally, using the prototype extension `handle` on Promise types is also clean and is arguably even more readable because it also uses the chaining pattern already standard when working with Promises 🌟 :
49
+
using the prototype extension `handle` on Promise types is a bit cleaner, and its potentially more readable because its also using the same chaining pattern already standard for working with Promises 🌟 :
64
50
65
51
```javascript
66
52
require('await-on');
@@ -71,6 +57,7 @@ async function foo(req,res) {
71
57
}
72
58
```
73
59
60
+
74
61
## Type fuzziness
75
62
Non-promises will passthrough same as the behavior of the native `await`
76
63
@@ -79,5 +66,20 @@ const [err,answer] = await on(42); //not a promise but ok no big deal
79
66
console.log(answer) //> 42
80
67
```
81
68
69
+
70
+
## Decorator approach
71
+
A decorator `on.handler` is also provided to wrap promise bearing functions with the handling functionalities:
72
+
73
+
```javascript
74
+
const {handler} =require('await-on');
75
+
letfetchData= () =>newPromise(/*...*/);
76
+
fetchDataAndHandle =handler(fetchData);
77
+
78
+
asyncfunctionfoo(req,res) {
79
+
const [err, data] =awaitfetchDataAndHandle();
80
+
!err ?res.send(data) :res.send(err);
81
+
}
82
+
```
83
+
82
84
## License
83
85
MIT License. See [License](https://github.com/bitstrider/await-on/blob/master/LICENSE) in the repository.
0 commit comments