ph-lock
v1.0.1
Published
基于Promise开发的并发锁
Downloads
1
Readme
安装
npm i ph-lock pnpm i ph-lock
export default
// 1. 直接导出
import lock from 'lock'
// 2.导出实例,lockInstance
import {lockInstance} from 'lock'
// lockInstance接收一个参数,表示最大并发数量,0 < num <= 20, 默认 6
const lock = lockInstance(5)
引入
import lock, {lockInstance} from 'lock'
const lock = require('lock')
使用
import lock from 'lock'
const forFn = async (num) => {
const res = await lock(function() {
return new Promise(resolve => setTimeout(() => {
resolve(num)
}, 5000));
})
console.log('end----res:', res);
}
for (let i = 1; i <= 10; i++) {
forFn(i)
}
ts处理
.d.ts 文件
declare module '@without_end/lock'
发布
npm publish --registry=https://registry.npmjs.org