Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

express-async-wrap

1.0.0 • Public • Published

express-async-wrap

Allows the use of ES2016 async functions as Express route handlers.

Install

npm i express-async-wrap

Usage

To use in place of a normal route handler:

import wrap from 'express-async-wrap';
 
function makeResult(result) {
  return new Promise((resolve) => {
    setTimeout(() => resolve(result), 10);
  });
}
 
app.get('/', wrap(async function(req, res) {
  const results = [];
 
  for(let i = 0; i < 5; i++) {
    results.push(makeResult(`test${i}`));
  }
 
  res.send((await* results).join());
}));

To use as an error handler:

import wrap from 'express-async-wrap';
 
app.get('/', wrap(async function(req, res, next) {
  next(new Error('error'));
}));
app.use(wrap(async function(err, req, res, next) {
  res.status(500).send('error');
}));

install

npm i express-async-wrap

Downloadsweekly downloads

1,469

version

1.0.0

license

MIT

repository

githubgithub

last publish

collaborators

  • avatar