# promissary

> A tiny library for manually resolving promises

Latest version **0.2.0** (published 2020-02-02) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2020-02-02 |
| First published | 2019-12-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Paul |
| Maintainers | paulgoesdeep |
| Keywords | promise, async, wrapper |

## Links

- npm: https://www.npmjs.com/package/promissary
- Repository: https://github.com/velll/promissary
- npm.io page: https://npm.io/package/promissary

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

- 0.2.0 (latest) — 2020-02-02
- 0.1.4 — 2020-01-02
- 0.1.3 — 2019-12-18
- 0.1.2 — 2019-12-18
- 0.1.1 — 2019-12-18
- 0.1.0 — 2019-12-18

## README

# Promisssary
A library for manually resolving promises.

# Why?

I needed callbacks, but I didn't want to end up in a callback hell. I like Promises and this is what I wanted.

```
new SomeTask(<params>).run().then(val => (...));
// or
const val = await new SomeTask(<params>).run();
```

Q: ES6 Promises work. Why would anybody want to resolve them manually?

A: When using `requestAnimationFrame`, `setTimeout` or such you just don't have enough control to put everything in the Promise constructor. Well, you can of course, but I ended up with a messy code. So I built a wrapper around Promises to resolve them manually for this extra bit of flexibility.

# Usage
```
import Promissary from 'promissary';
```

For the love of god, please define a _promissary_ as private. You don't want anybody outside of your task to resolve it, do you?

```
class SomeTask {
  private finished: Promissary<boolean>;
  ...
}
```

Initialize it in a public method or a constructor
```
  constructor() {
    ...
    this.finished = new Promissary<boolean>();
    ...
  }
```

Do the actual asynchronous part in a private method and resolve.
```
  private doTheWork() {
    ...
    // however you're gonna check if it is done
    if (this.done()) {
      this.finished.resolve(true);
    }

    // as an example of why you might want to need to resolve the promise manually
    requestAnimationFrame(doTheWork.bind(this));
  }
```

Make a public method returning the promise
```
  public run() {
    this.doTheWork();
    return this.finished.promise;
  }
```

Now you can execute your asynchronous task with `then` or `await`.

# Typescript
It's written in typescript and the type definitions are included. Tbh, I don't think it's a good idea to use this in vanilla javascript. At least the typescript compiler does enforce private functions.

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