1.0.4 • Published 3 years ago

express-async-wrapped v1.0.4

Weekly downloads
5
License
MIT
Repository
github
Last release
3 years ago

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

3 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago