Skip to content

Commit a00b574

Browse files
committed
v0.1.2 | Switched the default export to on
1 parent a3a531f commit a00b574

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ really simple error handling with await/async
1111
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
1212

1313
## Overview
14-
This package basically provides 3 syntactical paths to choose from:
14+
This package basically provides 2 main syntaxes to choose from:
1515
```javascript
16-
const {on, handler} = require('await-on');
16+
const on = require('await-on');
1717

1818
const fetchData = () => new Promise(/*...*/);
1919

2020
const [err, data] = await on(fetchData());
21-
const [err, data] = await handler(fetchData)(); //decorator
2221
const [err, data] = await fetchData().handle(); //prototype extension
2322
```
2423

@@ -47,20 +46,7 @@ async function foo(req,res) {
4746
}
4847
```
4948

50-
Using the decorator pattern with `handler` yields some cleaner high level code:
51-
52-
```javascript
53-
const {handler} = require('await-on');
54-
let fetchData = () => new Promise(/*...*/);
55-
fetchData = handler(fetchData);
56-
57-
async function foo(req,res) {
58-
const [err, data] = await fetchData();
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 🌟 :
6450

6551
```javascript
6652
require('await-on');
@@ -71,6 +57,7 @@ async function foo(req,res) {
7157
}
7258
```
7359

60+
7461
## Type fuzziness
7562
Non-promises will passthrough same as the behavior of the native `await`
7663

@@ -79,5 +66,20 @@ const [err,answer] = await on(42); //not a promise but ok no big deal
7966
console.log(answer) //> 42
8067
```
8168

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+
let fetchData = () => new Promise(/*...*/);
76+
fetchDataAndHandle = handler(fetchData);
77+
78+
async function foo(req,res) {
79+
const [err, data] = await fetchDataAndHandle();
80+
!err ? res.send(data) : res.send(err);
81+
}
82+
```
83+
8284
## License
8385
MIT License. See [License](https://github.com/bitstrider/await-on/blob/master/LICENSE) in the repository.

lib/await-on.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ function on(promise) {
2222
* @param {Function} wrapped Function to be decorated
2323
* @return {Function} Function decorated
2424
*/
25-
const handler = (wrapped) => () => on(wrapped())
25+
on.handler = (wrapped) => () => on(wrapped())
2626

2727
//Promise type extension
2828
global.Promise.prototype.handle = function() {
2929
return on(this)
3030
}
3131

32-
module.exports = {handler,on}
32+
module.exports = on

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "await-on",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "really simple error handling with await/async",
55
"main": "lib/await-on.js",
66
"directories": {

test/core.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const tap = require('tap')
2-
const {handler,on} = require('../lib/await-on')
3-
2+
const on = require('../lib/await-on')
43
const ANSWER = 42
54
const ERROR = new Error('Need more time to figure that out')
65

@@ -19,7 +18,7 @@ tap.test("on", async function (t) {
1918
tap.test("@handler", async function (t) {
2019
t.plan(2)
2120

22-
const waitOneHandle = handler(waitOne)
21+
const waitOneHandle = on.handler(waitOne)
2322

2423
const [err,res] = await waitOneHandle()
2524

0 commit comments

Comments
 (0)