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

⚙️ Utility function to use async express handlers

License

Notifications You must be signed in to change notification settings

XPBytes/express-async-handler

Repository files navigation

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)
)

Releases

No releases published

Packages

No packages published