Skip to content

Commit caaf5e4

Browse files
committed
v0.1.0 | Base Implementation
- includes readme, unit test, ci config
1 parent 62638af commit caaf5e4

File tree

7 files changed

+3889
-0
lines changed

7 files changed

+3889
-0
lines changed

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test
2+
.nyc_output
3+
travis.yml

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 😪 await-on
2+
![npm](https://img.shields.io/npm/v/await-on.svg)
3+
![node](https://img.shields.io/node/v/await-on.svg)
4+
![npm](https://img.shields.io/npm/l/await-on.svg)
5+
![npm](https://img.shields.io/npm/dt/await-on.svg)
6+
![Travis](https://img.shields.io/travis/bitstrider/await-on.svg)
7+
8+
9+
really simple error handling with await/async
10+
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+
13+
## Overview
14+
This package basically provides 3 syntactical paths to choose from:
15+
```javascript
16+
const {on, handler} = require('await-on');
17+
18+
const fetchData = () => new Promise(/*...*/);
19+
20+
const [err, data] = await on(fetchData());
21+
const [err, data] = await handler(fetchData)(); //decorator
22+
const [err, data] = await fetchData().handle(); //prototype extension
23+
```
24+
25+
The goal is to avoid the built-in approach using the `try`/`catch` block pattern:
26+
27+
```javascript
28+
try{
29+
const data = await fetchData();
30+
res.send(data);
31+
}catch(err) {
32+
res.send(err);
33+
}
34+
```
35+
36+
## Quick Usage
37+
38+
using `on` with the function `fetchData` which returns a Promise that resolve to some result `data`:
39+
```javascript
40+
const {on} = require('await-on');
41+
const fetchData = () => new Promise(/*...*/);
42+
43+
async function foo(req,res) {
44+
const [err, data] = await on(fetchData());
45+
if(err) res.send(err);
46+
else res.send(data);
47+
}
48+
```
49+
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 🌟 :
64+
65+
```javascript
66+
require('await-on');
67+
68+
async function foo(req,res) {
69+
const [err, data] = await fetchData().handle();
70+
!err ? res.send(data) : res.send(err);
71+
}
72+
```
73+
74+
## Type fuzziness
75+
Non-promises will passthrough same as the behavior of the native `await`
76+
77+
```javascript
78+
const [err,answer] = await on(42); //not a promise but ok no big deal
79+
console.log(answer) //> 42
80+
```

lib/await-on.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
3+
/**
4+
* Handler which takes a promise and returns array signature with `[err, data]`.
5+
* @param {Promise} promise Promise to handle
6+
* @return {Array} Array with signature `[err, data]`
7+
*/
8+
function on(promise) {
9+
if(promise instanceof Promise) {
10+
return promise.then(data => {
11+
return [null, data];
12+
})
13+
.catch(err => [err]);
14+
15+
}else{
16+
return [null,promise]
17+
}
18+
}
19+
20+
/**
21+
* Decorator which takes a promise bearing function and returns that function wrapped with `on`.
22+
* @param {Function} wrapped Function to be decorated
23+
* @return {Function} Function decorated
24+
*/
25+
const handler = (wrapped) => () => on(wrapped())
26+
27+
//Promise type extension
28+
global.Promise.prototype.handle = function() {
29+
return on(this)
30+
}
31+
32+
module.exports = {handler,on}

0 commit comments

Comments
 (0)