https://github.com/lizhengnacl/hold-on
hold on, waiting for long-running operations
https://github.com/lizhengnacl/hold-on
await concurrence koa2
Last synced: about 2 months ago
JSON representation
hold on, waiting for long-running operations
- Host: GitHub
- URL: https://github.com/lizhengnacl/hold-on
- Owner: lizhengnacl
- Created: 2018-08-28T15:04:33.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-08-28T15:59:34.000Z (almost 8 years ago)
- Last Synced: 2025-03-09T22:39:52.807Z (over 1 year ago)
- Topics: await, concurrence, koa2
- Language: JavaScript
- Homepage: https://github.com/lizhengnacl/hold-on
- Size: 5.86 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## 背景
并发场景下,避免相同耗时操作`action`被重复触发,第`1`个请求触发`action`后,后续`n`个同质请求被`await`住,直到`action`执行结束,`n`个同质请求再继续执行,并直接使用`action`的结果。
## 示例
```
const HoldOn = require('koa2-hold-on');
const holdOn = new HoldOn();
// 判断是否进入耗时操作
const key = 'some key';
let status = holdOn.get(key);
if(status !== 'hold') {
holdOn.hold(key);
await action(); // 模拟耗时操作
holdOn.release(key);
}
await holdOn.wait(key);
// 结束耗时操作,继续流程
```