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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@awaitjs/express

v0.9.0

Published

Write Express middleware and route handlers using async/await

Downloads

35,025

Readme

@awaitjs/express

Write Express middleware and route handlers using async/await

Usage

const { addAsync } = require('@awaitjs/express');

// Or, if you want to use `wrap()`
const { wrap } = require('@awaitjs/express');

API

addAsync()

The addAsync() function is the preferred way to add async/await support to your Express app. This function adds several helper functions to your Express app.

It adds useAsync(), getAsync(), etc. to your Express app

The addAsync() function adds useAsync(), deleteAsync(), getAsync(), headAsync(), paramAsync(), patchAsync(), postAsync(), and putAsync().

const express = require('express');
const app = addAsync(express());

// `useAsync()` is like `app.use()`, but supports async functions
app.useAsync(async function(req, res, next) {
  await new Promise(resolve => setTimeout(resolve, 50));
});

// `getAsync()` is like `app.get()`, but supports async functions
app.getAsync('*', async function(req, res, next) {
  throw new Error('Oops!');
});

// Because of `getAsync()`, this error handling middleware will run.
// `addAsync()` also enables async error handling middleware.
app.use(function(error, req, res, next) {
  res.send(error.message);
});

const server = await app.listen(3000);

Router()

This module exports a Router() function that is a drop-in replacement for express.Router(), except the returned router has getAsync(), useAsync(), etc.

It exports a Router function that returns a new async-friendly router

const express = require('express');
const app = express(); // This app isn't async friendly.

const router = Router(); // But this router is.
router.getAsync('/foo', async function(req, res, next) {
  throw new Error('Oops!');
});

app.use(router);
app.use(function(err, req, res, next) {
  res.send(err.message);
});

const server = await app.listen(3000);

decorateApp

acquit:ignore:end

It is an alias for addAsync()

assert.equal(decorateApp, addAsync)

decorateRouter

It is an alias for addAsync()

assert.equal(decorateRouter, addAsync)

wrap()

If you need more fine-grained control than what addAsync() gives you, you can use the wrap() function. This function wraps an async Express middleware or route handler for better error handling.

It wraps an async function with Express-compatible error handling

const express = require('express');
const app = express();

// `wrap()` takes an async middleware or route handler and adds a
// `.catch()` to handle any errors. It also prevents double-calling
// `next()`.
app.get('*', wrap(async function(req, res, next) {
  throw new Error('Oops!');
}));

// `wrap()` also supports async error handling middleware.
app.use(wrap(async function(error, req, res, next) {
  throw new Error('foo');
}));

app.use(function(error, req, res, next) {
  res.send(error.message); // Will send back 'foo'
});

const server = await app.listen(3000);