Skip to content

Commit

Permalink
Support for async/await (#51)
Browse files Browse the repository at this point in the history
* First draft support for async/await

* Fixed tests using babel

* Re-enabled all tests

* Updated documentation

* wqFixes after review

* Version bump
  • Loading branch information
lmammino committed Nov 19, 2017
1 parent 74b9e2e commit a700f93
Show file tree
Hide file tree
Showing 9 changed files with 4,685 additions and 1,915 deletions.
9 changes: 9 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"presets": [
["env", {
"targets": {
"node": "6.10"
}
}]
]
}
49 changes: 45 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
<a href="https://standardjs.com/">
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
</a>
<a href="https://gitter.im/middyjs">
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
</a>
<a href="https://greenkeeper.io/">
<img src="https://badges.greenkeeper.io/middyjs/middy.svg" alt="Greenkeeper badge" style="max-width:100%;">
</a>
<a href="https://gitter.im/middyjs">
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
</a>
</p>
</div>


## TOC

- [A little appetizer](#a-little-appetizer)
- [Install](#install)
- [Requirements](#requirements)
Expand Down Expand Up @@ -247,6 +249,45 @@ to the user.
If no middleware manages the error, the lambda execution fails reporting the unmanaged error.


### Async Middlewares

Middy supports middlewares that return promises instead that directly calling the callback:

```javascript
const asyncValidator = () => {
before: (handler) => {
if (handler.event.body) {
return new Promise((resolve, reject) => {
// async validation logic
})
}

return Promise.resolve()
}
}

handler.use(asyncValidator())
```

Thanks to this behaviour you can define middlewares using `async` functions:

```javascript
const asyncValidator = () => {
before: async (handler) => {
if (handler.event.body) {
return await asyncValidate(handler.event.body)
}

return
}
}

handler.use(asyncValidator())
```

Of course, since AWS lambda runs on Node.js 6.10, you will need to transpile your `async/await` code (e.g. using [babel](https://babeljs.io/)).


## Writing a middleware

A middleware is an object that should contain at least 1 of 3 possible keys:
Expand Down Expand Up @@ -440,7 +481,7 @@ Middy factory function. Use it to wrap your existing handler to enable middlewar
| before | [<code>middlewareAttachFunction</code>](#middlewareAttachFunction) | attach a new *before-only* middleware |
| after | [<code>middlewareAttachFunction</code>](#middlewareAttachFunction) | attach a new *after-only* middleware |
| onError | [<code>middlewareAttachFunction</code>](#middlewareAttachFunction) | attach a new *error-handler-only* middleware |
| __middlewares | <code>Object</code> | contains the list of all the attached middlewares organised by type (`before`, `after`, `onError`). To be used only for testing and debugging purposes |
| __middlewares | <code>Object</code> | contains the list of all the attached middlewares organised by type (`before`, `after`, `onError`). To be used only for testing and debugging purposes |

<a name="useFunction"></a>

Expand Down
42 changes: 42 additions & 0 deletions README.md.hb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<a href="https://standardjs.com/">
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
</a>
<a href="https://greenkeeper.io/">
<img src="https://badges.greenkeeper.io/middyjs/middy.svg" alt="Greenkeeper badge" style="max-width:100%;">
</a>
<a href="https://gitter.im/middyjs">
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
</a>
Expand Down Expand Up @@ -246,6 +249,45 @@ to the user.
If no middleware manages the error, the lambda execution fails reporting the unmanaged error.
### Async Middlewares
Middy supports middlewares that return promises instead that directly calling the callback:
```javascript
const asyncValidator = () => {
before: (handler) => {
if (handler.event.body) {
return new Promise((resolve, reject) => {
// async validation logic
})
}
return Promise.resolve()
}
}
handler.use(asyncValidator())
```
Thanks to this behaviour you can define middlewares using `async` functions:
```javascript
const asyncValidator = () => {
before: async (handler) => {
if (handler.event.body) {
return await asyncValidate(handler.event.body)
}
return
}
}
handler.use(asyncValidator())
```
Of course, since AWS lambda runs on Node.js 6.10, you will need to transpile your `async/await` code (e.g. using [babel](https://babeljs.io/)).
## Writing a middleware
A middleware is an object that should contain at least 1 of 3 possible keys:
Expand Down
Loading

0 comments on commit a700f93

Please sign in to comment.