# imperative-promise

> A TS library for imperative promises.

Latest version **0.0.1** (published 2021-09-24) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2021-09-24 |
| First published | 2021-09-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 37.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | joaomilho |

## Links

- npm: https://www.npmjs.com/package/imperative-promise
- npm.io page: https://npm.io/package/imperative-promise

## Recent versions

- 0.0.1 (latest) — 2021-09-24

## README

# imperative-promise

A TS library for imperative promises.

## Why

In some rare use cases – like locks – one needs to be able to have a reference to a promise and resolve or reject it manually. 

But rolling your own imperative promise has its gotchas. For instance, once you lift the resolve method from a promise, node will not wait for it to either resolve or reject before exiting.

This library solves this and other problems.

## Install

```sh
yarn add imperative-promise
npm install imperative-promise
```

## Usage

Resolve:

```ts
import { ImperativePromise } from "imperative-promise";

const ip = new ImperativePromise<string>();

ip.resolve("OK");

const result = await ip.promise; // OK
```

Rejecting:

```ts
import { ImperativePromise } from "imperative-promise";

const ip = new ImperativePromise();

ip.reject(new Error("Boom"));

const result = await ip.promise; // throw Boom
```

Canceling:

```ts
import { ImperativePromise } from "imperative-promise";

const ip = new ImperativePromise();

ip.cancel();

const result = await ip.promise; // undefined
```

Timeout:

```ts
import { ImperativePromise } from "imperative-promise";

const ip = new ImperativePromise({ timeout: 1000 });

const result = await ip.promise; 

// 1s later, throw Timeout
```

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