Giter Site home page Giter Site logo

hapi-async-handler's Introduction

hapi-async-handler Build Status

Adds support for ES2017 async functions to hapi route handlers

npm package

ES7 Async Functions

ES7 introduces async functions, 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, generator functions (and the yield keyword), and co today. There are examples of both styles of use shown below.

Using hapi-async-handler

Registering the Plugin

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.

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 or a similar compiler.

Using co

You can also use co and generator functions without any source-code transformations:

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);
    }),
  },
});

hapi-async-handler's People

Contributors

ide avatar zaggino avatar grabbou avatar

Watchers

James Cloos avatar Hapinode avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.