npm.io
1.0.4 • Published 5 years ago

express-async-wrapped

Licence
MIT
Version
1.0.4
Deps
0
Size
3 kB
Vulns
0
Weekly
0

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