GithubHelp home page GithubHelp logo

danawoodman / express-async-wrapper Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 3 KB

Wrap async express routes so exceptions can be caught by middleware.

JavaScript 100.00%
express expressjs express-js route routes async asyncawait js javascript middleware

express-async-wrapper's Introduction

express-async-wrapper

Wrap async express routes so exceptions can be caught by middleware.

If you're using the awesome ES7/ES2017 async/await features in your express routes, you'll notice that when a route throws an exception it times out. This wrapper allows express to properly catch exceptions and pass them to the appropriate error handler instead.

This code was borrowed from the StrongLoop blog post on handling async/await routes in express, I take no credit for coming up with this solution, I just wanted an npm module to use in my apps.

Install

# With npm
npm install  --save express-async-wrapper

# With yarn
yarn add express-async-wrapper

Usage

If you have a async express route file, like so:

const wrap = require('express-async-wrapper')

module.exports = wrap(async (req, res) => {
  const msg = await someSlowNetworkThingy()
  res.send('Hello', msg)
})

Now your route properly returns a promise object for express that will catch any throw exceptions.

Changelog

v0.1.0

  • Initial release! ๐ŸŽ‰ ๐Ÿพ

Credits

Licensed under an MIT license by Dana Woodman.

Original source code by StrongLoop (see here)

Pull requests welcome!

express-async-wrapper's People

Contributors

danawoodman avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.