express-async-wrapped
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

express-async-wrapped

Function that turns an async (promise) function into a function that can be passed to express.js as a handler. Rejections will get forwarded to express.

The function exported by this module can be used to wrap any express.js handler type:

  • Route handlers.
  • Param handlers.
  • Middleware.
  • Error-handling middleware.
  • Arrays of any of the above.

Quick start

const asyncWrap = require('express-async-wrapped');

router.get('/foo', asyncWrap(async (req, res) => {
    res.json(await getSomeData(req));
}));

router.use(asyncWrap(async (req, res, next) => {
    // await ...

    next();
}));

router.param('foo', asyncWrap(async (req, res, next, id) => {
    req.foo = await getTheFoo(id);
    next();
}));

router.use(asyncWrap(async (err, req, res, next) => {
    // await ...

    res.status(500).end();
}));

/express-async-wrapped/

    Package Sidebar

    Install

    npm i express-async-wrapped

    Weekly Downloads

    1

    Version

    1.0.4

    License

    MIT

    Unpacked Size

    3.37 kB

    Total Files

    5

    Last publish

    Collaborators

    • cdhowie