Skip to content

Commit

Permalink
[README] Update readme to be more modern
Browse files Browse the repository at this point in the history
  • Loading branch information
James Ide committed Jul 2, 2016
1 parent 8c333ea commit 33458cb
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions README.md
@@ -1,11 +1,11 @@
# hapi-async-handler [![Build Status](https://travis-ci.org/ide/hapi-async-handler.svg?branch=master)](https://travis-ci.org/ide/hapi-async-handler)
Adds support for ES7 async functions to [hapi](http://hapijs.com/) route handlers
Adds support for ES2017 async functions to [hapi](http://hapijs.com/) route handlers

[![npm package](https://nodei.co/npm/hapi-async-handler.png?downloads=true&downloadRank=true&stars=true)](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 [io.js](https://iojs.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.
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

Expand All @@ -14,8 +14,8 @@ ES7 introduces [async functions](https://github.com/lukehoban/ecmascript-asyncaw
```javascript
var server = new Hapi.Server();
server.register([
require('hapi-async-handler')
], function(error) { ... });
require('hapi-async-handler'),
], (error) => { ... });
```

## Defining a Route Handler
Expand All @@ -27,22 +27,22 @@ server.route({
method: 'GET',
path: '/',
handler: {
// The "async" handler accepts an async function
async: async function(request, reply) {
// 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');
var fileContents = await fs.promise.readFile('example.txt', 'utf8');
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/usage/experimental/), [regenerator](https://facebook.github.io/regenerator/), or a similar compiler.
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 if you are using io.js.
You can also use co and generator functions without any source-code transformations:

```javascript
server.route({
Expand All @@ -54,7 +54,7 @@ server.route({
require('instapromise');
var fileContents = yield fs.promise.readFile('example.txt', 'utf8');
reply(fileContents);
})
}
}),
},
});
```

0 comments on commit 33458cb

Please sign in to comment.