# advanced-promises

> Advanced promise types

Latest version **1.0.0-beta.13** (published 2020-05-16) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install advanced-promises
pnpm add advanced-promises
yarn add advanced-promises
bun add advanced-promises
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0-beta.13 |
| Published | 2020-05-16 |
| First published | 2020-04-18 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 51.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Will Henderson |
| Maintainers | whenderson |
| Keywords | promise, promises, abortable, abortable promise, cancellable, cancellable promise, timeout, timeout promise, process, process promise, async, advanced-promises |

## Links

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

## 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-beta.13 (latest) — 2020-05-16
- 1.0.0-beta.12 — 2020-05-16
- 1.0.0-beta.11 — 2020-05-16
- 1.0.0-beta.10 — 2020-05-16
- 1.0.0-beta.9 — 2020-05-16
- 1.0.0-beta.8 — 2020-04-18
- 1.0.0-beta.7 — 2020-04-18
- 1.0.0-beta.6 — 2020-04-18
- 1.0.0-beta.5 — 2020-04-18
- 1.0.0-beta.4 — 2020-04-18
- 1.0.0-beta.2 — 2020-04-18
- 1.0.0-beta.1 — 2020-04-18

## README

# advanced-promises
Advanced promise types for handling cancellation and aborting.
Package has no external dependencies.

## Installation

Yarn

```shell script
yarn add advanced-promises
```

Npm

```shell script
npm add --save advanced-promises
```

## Examples

### Cancellable Promises
A cancellable promise allows you to stop the promise from ever resolving or rejecting.

Example:
        
    import { Timeout } from 'advanced-promises';
        
    // Construct a timeout for 1 second
    const timeout = new Timeout(1000);
    
    // Cancel the timeout
    timeout.cancel();
    
    // This will never resolve
    await timeout;  

### Abortable Promises
An abortable promise provides an API for the caller to request an early abort from the callee.
The callee can register handlers which will get run if the promise is aborted.

Example:

    import { Abortable } from 'advanced-promises';

    const abortable = new Abortable((resolve, reject, aapi) => {
        //.. start long running code ..
        
        aapi.on(async () => {
            //.. cancel long running code ..
        });
        
        //.. wait for long running code ...
        new Timeout(50).then(() => {
            resolve('real result');
        });
    });
    
    // request an abort and wait until handlers have run
    const res = await abortable.abortWith({ resolve: 'my value'});
    
    // will resolve (or reject) according to the provided input
    assert(res === 'my value');
    
    // wait for the wrapped promise to complete
    // EcmaScript has no facility for forcefully terminating a promise early
    // It's often best to wait for the internal promise
    const realRes = await abortable.promise;
    assert(realRes === 'real result');

### Deconstructed Promise
It is often useful to define the resolve/reject semantics of a promise outside of the executor function.
This promise type provides takes no executor and instead provides resolve/reject handlers as instance properties.

Example:

    import { Deconstructed } from 'advanced-promises';

    const deconstructed = new Deconstructed();
    
    deconstructed.resolve('my value');
    
    const res = await deconstructed;
    
    assert(res === 'my value');

### Detailed API documentation
See [Advanced Promise](https://whenderson.github.io/advanced-promises/) for detailed API documentation

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