# simple-promise

> Simple and fast promise factory for Node and web apps.

Latest version **1.3.4** (published 2014-07-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install simple-promise
pnpm add simple-promise
yarn add simple-promise
bun add simple-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.3.4 |
| Published | 2014-07-23 |
| First published | 2014-07-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Dan Cobb |
| Maintainers | cobbdb |
| Keywords | Promise, simple, webapp, dep, quick, bare, minimal, callback, attach |

## Links

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

## Dependencies (1)

- [underscore](https://npm.io/package/underscore.md) ^1.6.0

## 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

- 1.3.4 (latest) — 2014-07-23
- 1.3.2 — 2014-07-23
- 1.3.1 — 2014-07-22
- 1.3.0 — 2014-07-22
- 1.2.3 — 2014-07-22
- 1.2.1 — 2014-07-21
- 1.2.0 — 2014-07-21
- 1.1.1 — 2014-07-20
- 1.0.2 — 2014-07-17
- 1.0.1 — 2014-07-17
- 1.0.0 — 2014-07-17

## README

# simple-promise [![Bower version](https://badge.fury.io/bo/simple-promise.svg)](http://badge.fury.io/bo/simple-promise) [![NPM version](https://badge.fury.io/js/simple-promise.svg)](http://badge.fury.io/js/simple-promise)

Simple and fast promise factory for Node and web apps.

    $ bower install simple-promise
    $ npm install simple-promise

-------------
## API

#### promise(task)
- **Param** task *Function* Entry point of the promise.
- **Returns** *Function*
- `var say = promise(function (msg, name) {`

  #### task([{args}], done)
  - **Param** [args] *Any* Task arguments.
  - **Param** done *Function* Call to signal end of task.
  - **Returns** *Any* Return value of `task`.
  - `var say = promise(function (msg, name, done) {`

    #### done([{args}])
    - **Param** [args] *Any* Done arguments.
    - **Returns** *Any* Return value of `then` contract.*
    - `var thenResult = done('abc123');`

#### promise.then(contract)
- **Param** contract *Function* Called on task's `done`.
- **Returns** *Any* Return value of `then` contract.
- `say.then(function (async, sync, msg, name) {`

  #### contract([{async}], [sync], {[args]})
  - **Param** async *Any* Call argument(s) of task's `done`.
  - **Param** sync *Any* Return value of `task`.
  - **Returns** *Any* Return value of `then` contract.
  - `say.then(function (async1, async2, sync, msg, name) {`

#### promise.error(contract)
- **Param** contract *Function* Called on `task` error.
- **Returns** *Any* Return value of `error` contract.
- `say.error(function (err, msg, name) {`

  #### contract(err, {[args]})
  - **Param** err *Error* Error object thrown in `task`.
  - **Param** [args] *Any* All `task` arguments.
  - **Returns** *Any* Return value of `error` contract.
  - `say.error(function (err, msg, name) {`

-------------
## Code Samples
Here are some quick code samples to help you get started.

### Load the library
Simple-Promise is a CommonJS library, so the require statement can
be used for both Node **and** web applications!

    var promise = require('simple-promise');

#### Creating a new promise
At their core, promises look and behave like a normal function.

    var greet = promise(function (name, done) {
        console.log('Hello %s!', name);
        done();
    });

#### Attach a success behavior
Callbacks can be a hassle and quickly create a mess. Tackle the common usage of callbacks with
a promise instead. The `then` function is optional and is called immediately after successful
completion of the promise.

    greet.then(function (name) {
        console.log('Farewell %s!', name);
    });

#### Attach an error behavior
Sometimes things don't go as expected. Attach an optional error behavior to handle any
problems.

    greet.error(function (err, name) {
        console.log('%s caused an error!', name);
        console.error('%s : %s', err.name, err.message);
    });

#### Chain your method calls
Each method supports chaining for quick and clean instantiation.

    promise(function (done) {
        console.log('First, this happened.');
        done();
    }).then(function () {
        console.log('Then, this happened.');
    }).error(function () {
        console.error("Hopefully this won't happen to you.");
    });

#### Immediately invoke your promise
You can invoke immediately with the `run` method or parens.

    var go = promise(function (greeting, name) {
        console.log('%s %s!', greeting, name);
    });
    go.run('Hello', 'World');
    // ~ or ~ like this:
    go('Hello', 'World');

#### Collect all return values
Return values are passed along the chain so you can use them
however you need.

    var result;
    promise(function (name, done) {
        // Some async action.
        setTimeout(function () {
            result = done();
        }, 100);
        return 'Hello!';
    }).then(function (sync, name) {
        return name + ' says ' + sync;
    }).run('Tom');

    var result = promise(function (name) {
        throw Error('Hello!');
    }).error(function (err, name) {
        return name + ' says ' + err.message;
    }).run('Tom');

Both of these blocks will eventually output the same string;
`result` will equal `Tom says Hello!`

---------
* See: http://github.com/cobbdb/simple-promise
* License: MIT

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