https://github.com/ide/hapi-async-handler
Adds support for ES7 async functions to hapi route handlers
https://github.com/ide/hapi-async-handler
Last synced: 11 months ago
JSON representation
Adds support for ES7 async functions to hapi route handlers
- Host: GitHub
- URL: https://github.com/ide/hapi-async-handler
- Owner: ide
- License: mit
- Created: 2015-02-06T00:44:04.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2017-02-21T02:53:17.000Z (over 9 years ago)
- Last Synced: 2025-06-29T17:04:04.883Z (12 months ago)
- Language: JavaScript
- Size: 12.7 KB
- Stars: 44
- Watchers: 2
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hapi-async-handler [](https://travis-ci.org/ide/hapi-async-handler)
Adds support for ES2017 async functions to [hapi](http://hapijs.com/) route handlers
[](https://nodei.co/npm/hapi-async-handler/)
# ES7 Async Functions
ES7 introduces [async functions](https://github.com/lukehoban/ecmascript-asyncawait), which are functions that support the `await` keyword and return promises. This hapi plugin adds a handler called `async` that allows you to write your route handlers using async functions. You can also use hapi-async-handler with [Node.js](https://nodejs.org/), generator functions (and the `yield` keyword), and [co](https://github.com/tj/co) today. There are examples of both styles of use shown below.
# Using hapi-async-handler
## Registering the Plugin
```javascript
var server = new Hapi.Server();
server.register([
require('hapi-async-handler'),
], (error) => { ... });
```
## Defining a Route Handler
Define an async function that receives `request` and `reply` like a normal route handler and assign it the `async` property of the route handler.
```javascript
server.route({
method: 'GET',
path: '/',
handler: {
// Define a property called "async" that's an async function
async async(request, reply) {
// instapromise gives you promises from methods with Node-style callbacks
require('instapromise');
let fileContents = await fs.promise.readFile('example.txt', 'utf8');
reply(fileContents);
},
},
});
```
For the `async` keyword to work, you will need to transform your source code with [Babel](https://babeljs.io/docs/plugins/preset-stage-3/) or a similar compiler.
### Using co
You can also use co and generator functions without any source-code transformations:
```javascript
server.route({
method: 'GET',
path: '/',
handler: {
// co.wrap creates a function that returns a promise, just like an async function
async: co.wrap(function*(request, reply) {
require('instapromise');
var fileContents = yield fs.promise.readFile('example.txt', 'utf8');
reply(fileContents);
}),
},
});
```