# @typeshell/promise

> ## install

Latest version **0.0.5** (published 2021-06-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install @typeshell/promise
pnpm add @typeshell/promise
yarn add @typeshell/promise
bun add @typeshell/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.5 |
| Published | 2021-06-05 |
| First published | 2021-06-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 20.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | amslime, x-cmd, edwinjhlee, lteam |

## Links

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

## Dependencies (2)

- [@typeshell/iterator](https://npm.io/package/@typeshell/iterator.md) 0.0.1
- [@typeshell/semantic](https://npm.io/package/@typeshell/semantic.md) latest

## Recent versions

- 0.0.5 (latest) — 2021-06-05
- 0.0.4 — 2021-06-03
- 0.0.3 — 2021-06-03
- 0.0.2 — 2021-06-03
- 0.0.1 — 2021-06-03

## README

# promise-utils

## install

```bash
npm install @typeshell/promise --save
```


## 功能


### sleep函数，以及可中断

我可以提供一个 naive版本的

```typescript
const p = sleep(3000)
setTimeout(() => p.interrupt(), 1000)
await p // one second it will be interrupted
```

sleep的返回的对象本质是一个可中断的TimeoutPromise

### BlockingQueue

```typescript
import { BlockingQueue, sleep } from "../src"

const waitRandomly = () => sleep(Math.trunc(Math.random() * 100 + 30))

const bq = new BlockingQueue<number>(1)
const n = 10

const producer_main = async () => {
    try {
        for (let i = 0; i < n; ++i) {
            console.log(`put: ${i}`)
            await bq.put(i)
            console.log(`put.result: ${i}`)
            await waitRandomly()
        }
        bq.shutdown()
        console.log("producer_main: shutdown")
    } catch (ex) {
        console.log(ex)
    }
}
producer_main()

const consumer_main = async () => {
    try {
        for (let i = 0; i < n + 10; ++i) {
            console.log(`take.ready: ${i}`)
            // console.log(bq)
            const e = await bq.take()
            console.log(`take.result: ${i} ${JSON.stringify(bq.data)}`)
            await waitRandomly()
        }
        console.log("consumer_main: shutdown")
    } catch (ex) {
        console.log("Get shutdown error")
        console.log(ex)
    }
}
consumer_main()
```

## ExecutorQueue

```typescript

```


## 其它

### 为什么不做n2n队列

1. 生产者蜂拥而至，处于await状态，此时其实排在系统内部的队列，本质与我们放在队列排队，无性能区别
2. 多个生产者在等候，如果无队列化，一定会出现争抢，效率不高

因此 n2n 就用 `n->1` 以及 `1->n` 来模拟，避免性能

特性：

1. n个prodcuer，n个consumer
n个prodcuer会

1. 设置是否blocking
2. 传入函数，n个兑现
3. consumer.take


### 测试

采用长度为1的blockingqueue，非常容易重现死锁的问题

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