# p-single

> 使promise在执行完毕前不会再次执行

Latest version **1.0.9** (published 2018-07-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install p-single
pnpm add p-single
yarn add p-single
bun add p-single
```

## 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.9 |
| Published | 2018-07-24 |
| First published | 2018-02-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | herbluo |
| Maintainers | herbluo |
| Keywords | promise, singleton, debounce, throttle |

## Links

- npm: https://www.npmjs.com/package/p-single
- Repository: https://github.com/HerbLuo/p-single
- Homepage: https://github.com/HerbLuo/p-single#readme
- Issues: https://github.com/HerbLuo/p-single/issues
- npm.io page: https://npm.io/package/p-single

## 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.9 (latest) — 2018-07-24
- 1.0.8 — 2018-07-24
- 1.0.7 — 2018-07-24
- 1.0.6 — 2018-07-23
- 1.0.5 — 2018-07-23
- 1.0.4 — 2018-07-23
- 1.0.3 — 2018-07-23
- 1.0.2 — 2018-07-23
- 1.0.1 — 2018-02-22
- 1.0.0 — 2018-02-22

## README

### 简介

pSingle 用于处理异步操作  
pSingle 包裹的异步操作在完成前不会再次执行。   
只有当 pSingle 包裹的异步操作执行完毕后，pSingle才会使其可能再次运行  

该方法可减少api的前置（如登陆）或者后置（如刷新token）请求数量。


### 安装
```bash
npm install --save p-single
```

### 使用方法

例1：pSingle 接受一个参数，该参数是一个方法，该方法需返回Promise。  

```javascript 
import pSingle from './p-single.js'

// pSingle需要一个返回 Promise的方法
const doWhenClick = pSingle(function promiseReturnedFunc() {
  return new Promise(/*...*/)
})

// 或者直接使用 async
const getAccessToken = pSingle(async (username, password) => {
  // ...
})
```

例2：如果你使用了 ES Next - decorator，你可以这样使用 pSingle

```javascript 
import {PSingle} from './p-single.js'

class Api {
  /**
   * 刷新 token
   * 该请求直到返回前不会再次发送，
   * 该请求返回结果后，每一个调用者都会收到正确的 Promise信号
   */
  @PSingle()
  oauthByRefreshToken (refreshToken) {
    return fetch(/*...*/)
      // ...
  }
}
export default new Api()
```

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