Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

Latest commit

 

History

History
37 lines (29 loc) · 1.11 KB

README.md

File metadata and controls

37 lines (29 loc) · 1.11 KB

Express Async Handler

npm NPM Package Version Maintainability

Utility function to use async functions as express handlers

yarn add @xpbytes/express-async-handler
import { asyncHandler } from '@xpbytes/express-async-handler'

app.get(
  '/test',
  asyncHandler(async (req, res, next) => {
    const code = await Promise.resolve(204)
    res.sendStatus(code).end()
  })
)

You can optionally give a second argument errorHandler:

function onError(err, req, res, next) {
  // ...
}

app.get(
  '/test',
  asyncHandler(async (req, res, next) => {
    const code = await Promise.resolve(204)
    res.sendStatus(code).end()
  }, onError)
)