# or-promise

> Easily support returning a promise when a callback is omitted

Latest version **1.0.0** (published 2016-09-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install or-promise
pnpm add or-promise
yarn add or-promise
bun add or-promise
```

## 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.0 |
| Published | 2016-09-02 |
| First published | 2016-09-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Jason Pincin |
| Maintainers | jasonpincin |
| Keywords | promise, callback |

## Links

- npm: https://www.npmjs.com/package/or-promise
- Repository: https://github.com/jasonpincin/or-promise
- Issues: https://github.com/jasonpincin/or-promise/issues
- npm.io page: https://npm.io/package/or-promise

## 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.0 (latest) — 2016-09-02

## README

# or-promise

[![NPM version](https://badge.fury.io/js/or-promise.png)](http://badge.fury.io/js/or-promise)
[![Build Status](https://travis-ci.org/jasonpincin/or-promise.svg?branch=master)](https://travis-ci.org/jasonpincin/or-promise)
[![Coverage Status](https://coveralls.io/repos/jasonpincin/or-promise/badge.png?branch=master)](https://coveralls.io/r/jasonpincin/or-promise?branch=master)

Accept a callback OR return a promise (easily).

## example

```javascript
const orPromise = require('or-promise')

function greetSoon (cb) {
    cb = orPromise(cb)
    setImediate(cb, null, 'hello')

    // this will return undefined if cb was provided, otherwise
    // it will return a promise
    return cb.promise
}

// we can now use our fancy function either way...
greetSoon(function (err, msg) {
    if (err) return console.error(err)
    console.log(msg)
})

// or a promise
greetSoon().then(console.log).catch(console.error)
```

## example with default parameters

Same function as above, but using default parameters (if your JS engine supports
it):

```javascript
const orPromise = require('or-promise')

function greetSoon (cb = orPromise()) {
    setImediate(cb, null, 'hello')
    return cb.promise
}
```

## api

```javascript
var orPromise = require('or-promise')
```

### cb = orPromise([cb])

When called with a (truthy) `cb` argument, `orPromise` simply returns the 
argument it was called with, unaltered. 

If called with no argument, a function will be returned that is usable as an 
error-first callback. This function will have a `promise` property. When the
returned callback is invoked, the `promise` property will be:

* resolved if the callback was invoked with a falsy first argument
* rejected if the callback was invoked with a truthy first argument

If resolved, the resolved value will be the 2nd argument supplied to the
callback. If rejected, the error will be the 1st argument supplied to the
callback.

## testing

`npm test`

### browser test

`npm run browser-test`

This will run the tests in all browsers (specified in .zuul.yml). Be sure to 
[educate zuul](https://github.com/defunctzombie/zuul/wiki/cloud-testing#2-educate-zuul) 
first.

### coverage

`npm run open-cover`

Open coverage in browser.

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