npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

route-async

v1.0.8

Published

A route-wrapper allowing use of async / await syntax in Express route controllers

Readme

route-async

A route-wrapper allowing use of async / await syntax in Express route controllers.

NPM

To Use

npm install route-async

Wrap an async route

Assuming you have some async helper function called someAsync, you might have a route looking a bit like this:

const asyncRoute = require('route-async')
const someAsync = require('./helpers/someAsync')

const myRoute = async (req, res) => {
  const result = await someAsync(req.body)
  res.json(result)
}

module.exports = asyncRoute(myRoute)

The asyncRoute wrapper simply takes your route and wraps it, such that the async promise is either resolved internally, or if rejected a next function is called. The default next is just console.error and you can supply your own.

What about if my route wants next

Your route should not attempt to handle its own errors, but simply throw an Error, or even better an HttpError that gets caught by the async-route wrapper.

This keeps your core route code much simpler.

Testing async routes

The following example leverages mocha, sinon, and proxyquire to unit test the above route by supplying a spy in the place of the next function.

const { expect } = require('chai')
const { spy, stub } = require('sinon')
const proxyquire = require('proxyquire')

describe('src/routes/myRoute', () => {
  const mockSomeAsync = stub()

  const myRoute = proxyquire('../../src/routes/myRoute', {
    './helpers/someAsync': mockSomeAsync
  })

  const req = { body: 'some body' }
  const res = { json: stub() }
  const next = spy()

  const resetStubs = () => {
    res.json.resetHistory()
    next.resetHistory()
  }

  context('no errors', () => {
    const result = 'some result'

    before(async () => {
      mockSomeAsync.resolves(result)
      await myRoute(req, res, next)
    })

    after(resetStubs)

    it('called someAsync with the right data', () => {
      expect(mockSomeAsync).to.have.been.calledWith(req.body)
    })

    it('called res.json with the right data', () => {
      expect(res.json).to.have.been.calledWith(result)
    })

    it("didn't call next", () => {
      expect(next).not.to.have.been.called
    })
  })

  context('has errors', () => {
    const error = 'some error'

    before(async () => {
      mockSomeAsync.rejects(error)
      await myRoute(req, res, next)
    })

    after(resetStubs)

    it('called someAsync with the right data', () => {
      expect(mockSomeAsync).to.have.been.calledWith(req.body)
    })

    it("didn't call res.json", () => {
      expect(res.json).not.to.have.been.called
    })

    it('called next with the error', () => {
      expect(next).to.have.been.calledWith(error)
    })
  })
})

Development

Branches

| Branch | Status | Coverage | Audit | Comments | | -------- | ------- | --------- | ----- | --------- | | develop | CircleCI | codecov | Vulnerabilities | Work in progress | | main | CircleCI | codecov | Vulnerabilities | Latest release |

Prerequisites

  • NodeJS. I use nvm to manage Node versions — brew install nvm.

Initialisation

npm install

Test it

  • npm test — runs the unit tests
  • npm run test:unit:cov — runs the tests with code coverage output.

Lint it

npm run lint

Contributing

Please see the contributing notes.