# make-promises-safe

> Crash or abort if you get an unhandledRejection or multipleResolves

Latest version **5.1.0** (published 2019-10-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install make-promises-safe
pnpm add make-promises-safe
yarn add make-promises-safe
bun add make-promises-safe
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.1.0 |
| Published | 2019-10-02 |
| First published | 2017-06-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 663 |
| Author | Matteo Collina |
| Maintainers | matteo.collina |
| Keywords | promise, promises, safe, unhandled, rejection, unhandledRejection |

## Links

- npm: https://www.npmjs.com/package/make-promises-safe
- Repository: https://github.com/mcollina/make-promises-safe
- Homepage: https://github.com/mcollina/make-promises-safe#readme
- Issues: https://github.com/mcollina/make-promises-safe/issues
- npm.io page: https://npm.io/package/make-promises-safe

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 5.1.0 (latest) — 2019-10-02
- 5.0.0 — 2019-03-21
- 4.0.0 — 2018-11-20
- 3.0.0 — 2018-11-01
- 2.0.0 — 2018-10-25
- 1.1.0 — 2017-06-22
- 1.0.1 — 2017-06-19
- 1.0.0 — 2017-06-19

## README

# make-promises-safe &nbsp;&nbsp;[![Build Status](https://travis-ci.org/mcollina/make-promises-safe.svg?branch=master)](https://travis-ci.org/mcollina/make-promises-safe)

A node.js module to make the use of promises safe.
It implements the deprecation [DEP0018][unhandled] of Node.js in versions 6+.
Using Promises without this module might cause file descriptor and memory
leaks.

**It is important that this module is only used in top-level program code, not
in reusable modules!**

## The Problem

Node.js crashes if there is an uncaught exception, while it does not
crash if there is an `'unhandledRejection'`, i.e. a Promise without a
`.catch()` handler.

**If you are using promises, you should attach a `.catch()` handler
synchronously**.

As an example, the following server will leak a file descriptor because
of a missing `.catch()`  handler:

```js
const http = require('http')
const server = http.createServer(handle)

server.listen(3000)

function handle (req, res) {
  doStuff()
    .then((body) => {
      res.end(body)
    })
}

function doStuff () {
  if (Math.random() < 0.5) {
    return Promise.reject(new Error('kaboom'))
  }

  return Promise.resolve('hello world')
}
```

## The Solution

`make-promises-safe` installs an `process.on('unhandledRejection')`
handler that prints the stacktrace and exits the process with an exit
code of 1, just like any uncaught exception.

## Install

```
npm install make-promises-safe --save
```

## Usage

```js
'use strict'

require('make-promises-safe') // installs an 'unhandledRejection' handler
const http = require('http')
const server = http.createServer(handle)

server.listen(3000)

function handle (req, res) {
  doStuff()
    .then((body) => {
      res.end(body)
    })
}

function doStuff () {
  if (Math.random() < 0.5) {
    return Promise.reject(new Error('kaboom'))
  }

  return Promise.resolve('hello world')
}
```

### as a preloader

You can add this behavior to any Node.js application by using it as a
preloader:

```
node -r make-promises-safe server.js
```

### with core dumps

You can also create a core dump when an unhandled rejection occurs:


```
require('make-promises-safe').abort = true
```

### With custom logging

You can add a custom logger to log errors in your own format. To do this override the `logError` property with a function that takes a single `Error` parameter. This defaults to `console.error`.

```
const makePromisesSafe = require('make-promises-safe');
makePromisesSafe.logError = function(err) {
  // log the err object
}
```

## License

MIT

[unhandled]: https://nodejs.org/dist/latest-v8.x/docs/api/deprecations.html#deprecations_dep0018_unhandled_promise_rejections

---
_Source: https://npm.io/package/make-promises-safe · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
