# promise-executor

> A promise executor

Latest version **0.1.1** (published 2022-01-13) · MIT license · 0 weekly downloads

## Install

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

## 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.1.1 |
| Published | 2022-01-13 |
| First published | 2022-01-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 21.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Gurwinder Singh |
| Maintainers | 95th |

## Links

- npm: https://www.npmjs.com/package/promise-executor
- Repository: https://github.com/95th/promise-executor
- Homepage: https://github.com/95th/promise-executor#readme
- Issues: https://github.com/95th/promise-executor/issues
- npm.io page: https://npm.io/package/promise-executor

## Recent versions

- 0.1.1 (latest) — 2022-01-13
- 0.1.0 — 2022-01-13

## README

# promise-executor

A promise executor which returns the next value to be resolved. The results are ordered according to their completion.

User can call `submit(Promise)` to add a promise

and call `next()` to get a promise that will resolve next.

## Usage:

```typescript
const exec = new PromiseExecutor<number>();
exec.submit(Promise.resolve(1));
exec.submit(Promise.resolve(2));

const firstCompleted = await exec.next();
const secondCompleted = await exec.next();
```

## Building:

```sh
# Build
npm run build

# Test
npm run test
```

This library is useful to build logic which require limited concurrency.

For example: You have 20 tasks to perform but you want to limit the concurrent tasks to 4.

```typescript
const exec = new PromiseExecutor();
const concurrency = 4;
let i = 0;
let totalTasks = 20;

while (true) {
    if (exec.length == 0 && i == totalTasks) {
        // Nothing pending and nothing more to add.
        break;
    }
    // Add tasks if below target concurrency.
    while (exec.remaining < concurrency && i < totalTasks) {
        exec.submit(getTask(i));
        i++;
    }
    // Wait for one of the tasks to complete.
    const result = await exec.next();
    if (result) {
        useResult(result.value);
    }
}
```

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