Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including express-rescue with all npm packages installed. Try it out:

var expressRescue = require("express-rescue")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

express-rescue v1.1.26

A wrapper for async functions which makes sure all async errors are passed to your errors handler

JavaScript Style Guide Coverage Status Build Status


Express Rescue

It's a wrapper for async functions which makes sure all async errors are passed to your error handler preventing those anoing UnhandledPromiseRejectionWarning when using async/await, especially with expressjs routes/middlewares. And nope, this is not an anti-pattern.

Usin with expressjs

It's so simple... how about I just show you how it's done?

const rescue = require('express-rescue')

// ...

app.get('/:id', rescue(async (req, res, next) => {
    const user = await repository.getById(

    if (!user) {
      throw new UserNotFoundError


app.use((err, req, res, next) => {
  if (err instanceof UserNotFoundError) {
    return res.status(404)
              .json({ error: 'these are not the droids you\'re looking for'})

     .json({ error: 'i have a bad feeling about this'})

That's all. Told you it was simple, right?

Using anywhere else

It was intended to be use with expressjs but I'm pretty sure you could use it anywhere you want since you pass your error handler as the last argument.

Let's try it out:

const rescue = require('express-rescue')

const somethingWrong = rescue(function (arg1, arg2, arg3, arg4, errorHandler) {
  throw new Error('Houston, we have a problem!')

const arg1 = null
const arg2 = null
const arg3 = null
const arg4 = null

const errorHandler = function (err) {

somethingWrong(arg1, arg2, arg3, arg4, errorHandler) // Houston, we have a problem!

Oh in case you didn't notice: yes, you're not limited to use only async functions. somethingWrong is a regular (sync) function.

Hope you enjoy the async/await heaven!



yarn test v0.27.5
$ mocha specs --require ./specs/spec-helper.js

  const callable = rescue(async ([err,] req, res, next) => { })
    calls the last argument (next) with the thrown error
      ✓ Raises a TypeError if last argument is not a function
      ✓ callable(req, res, next) - works for routes and middlewares
      ✓ callable(err, req, res, next) - works for error handler middlewares
      ✓ callable(foo, bar, baz, foobar, foobaz, errorHandler) - should work for basically anything, since you place an error handler as the last argument

  4 passing (17ms)

Done in 0.48s.
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free