{"id":18013710,"url":"https://github.com/ide/hapi-async-handler","last_synced_at":"2025-07-13T17:09:48.204Z","repository":{"id":26924622,"uuid":"30386810","full_name":"ide/hapi-async-handler","owner":"ide","description":"Adds support for ES7 async functions to hapi route handlers","archived":false,"fork":false,"pushed_at":"2017-02-21T02:53:17.000Z","size":13,"stargazers_count":44,"open_issues_count":2,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-29T17:04:04.883Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ide.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-06T00:44:04.000Z","updated_at":"2023-11-03T17:11:28.000Z","dependencies_parsed_at":"2022-08-31T09:42:02.349Z","dependency_job_id":null,"html_url":"https://github.com/ide/hapi-async-handler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ide/hapi-async-handler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ide%2Fhapi-async-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ide%2Fhapi-async-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ide%2Fhapi-async-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ide%2Fhapi-async-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ide","download_url":"https://codeload.github.com/ide/hapi-async-handler/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ide%2Fhapi-async-handler/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265175567,"owners_count":23722661,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-30T03:22:46.507Z","updated_at":"2025-07-13T17:09:48.182Z","avatar_url":"https://github.com/ide.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hapi-async-handler [![Build Status](https://travis-ci.org/ide/hapi-async-handler.svg?branch=master)](https://travis-ci.org/ide/hapi-async-handler)\nAdds support for ES2017 async functions to [hapi](http://hapijs.com/) route handlers\n\n[![npm package](https://nodei.co/npm/hapi-async-handler.png?downloads=true\u0026downloadRank=true\u0026stars=true)](https://nodei.co/npm/hapi-async-handler/)\n\n# ES7 Async Functions\n\nES7 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.\n\n# Using hapi-async-handler\n\n## Registering the Plugin\n\n```javascript\nvar server = new Hapi.Server();\nserver.register([\n  require('hapi-async-handler'),\n], (error) =\u003e { ... });\n```\n\n## Defining a Route Handler\n\nDefine an async function that receives `request` and `reply` like a normal route handler and assign it the `async` property of the route handler.\n\n```javascript\nserver.route({\n  method: 'GET',\n  path: '/',\n  handler: {\n    // Define a property called \"async\" that's an async function\n    async async(request, reply) {\n      // instapromise gives you promises from methods with Node-style callbacks\n      require('instapromise');\n      let fileContents = await fs.promise.readFile('example.txt', 'utf8');\n      reply(fileContents);\n    },\n  },\n});\n```\n\nFor 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.\n\n### Using co\n\nYou can also use co and generator functions without any source-code transformations:\n\n```javascript\nserver.route({\n  method: 'GET',\n  path: '/',\n  handler: {\n    // co.wrap creates a function that returns a promise, just like an async function\n    async: co.wrap(function*(request, reply) {\n      require('instapromise');\n      var fileContents = yield fs.promise.readFile('example.txt', 'utf8');\n      reply(fileContents);\n    }),\n  },\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fide%2Fhapi-async-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fide%2Fhapi-async-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fide%2Fhapi-async-handler/lists"}