# p2n

> A very lightweight way to nodefiy a promise.

Latest version **1.0.1** (published 2017-12-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install p2n
pnpm add p2n
yarn add p2n
bun add p2n
```

## 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 | 1.0.1 |
| Published | 2017-12-09 |
| First published | 2017-12-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | James Talmage |
| Maintainers | jamestalmage |
| Keywords | promise, nodeify, callback |

## Links

- npm: https://www.npmjs.com/package/p2n
- Repository: https://github.com/jamestalmage/p2n
- Homepage: https://github.com/jamestalmage/p2n#readme
- Issues: https://github.com/jamestalmage/p2n/issues
- npm.io page: https://npm.io/package/p2n

## Dependencies (1)

- [global](https://npm.io/package/global.md) ^4.3.2

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@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

## Recent versions

- 1.0.1 (latest) — 2017-12-09
- 1.0.0 — 2017-12-09

## README

# p2n [![Build Status](https://travis-ci.org/jamestalmage/p2n.svg?branch=master)](https://travis-ci.org/jamestalmage/p2n) [![codecov](https://codecov.io/gh/jamestalmage/p2n/badge.svg?branch=master)](https://codecov.io/gh/jamestalmage/p2n?branch=master)

> A very lightweight way to nodeify a promise.

"Nodebacks", "Errorbacks", or just "error first callbacks" -- whatever you want to call them. It seems relatively straightforward to convert from a promise to a node style callback:

```js
promise.then(
	result => cb(null, result),
	err => cb(err)
);
```

However, if `cb` throws, that results in an unhandled promise rejection. The error will not bubble up and crash the node process, or hit the browser console in older browsers. This breaks the typical contract of node callbacks, and can lead to hidden, hard to find bugs. The tendency of Promises to silently swallow errors earned them plenty of detractors in the Node community.

This module handles all the above problems, and works on Node and in the browser. It uses `setImmediate` in Node to throw errors outside the promise stack, and falls back to `setTimeout` in the browser. This is the exact same mechanism Promise libraries like `Bluebird` use to "nodeify" promises, but without the bulk of those huge libraries.
 
Note: If you are running this in the browser, and throwing many, many errors each second, you *may* want to [polyfill `setImmediate`](https://github.com/YuzuJS/setImmediate). We avoid including it by default because it adds unnecessary bulk to your browser bundle if errors are infrequent.

## Install

```
$ npm install p2n
```


## Usage

```js
const p2n = require('p2n');

// allows for a flexible api, users can choose promises or callbacks. 
function myApi(args, optionalNodeback) {
	// ...
	
	// returns "somePromise", and attaches the callback if was provided.
	return p2n(somePromise, optionalNodeback);
}
```


## API

### p2n(promise, [callback])

Returns the promise (useful for chaining). 

#### promise

Type: `Promise`

The promise to attach the callback to.

#### optionalCallback

Type: `function(err, result)`<br>
Optional

If provided, the callback will be called when the Promise resolves.


## License

MIT © [James Talmage](https://github.com/jamestalmage)

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