lockx
v0.0.5
Published
[新API]锁住你的代码片段,缓存等待队列,控制并发
Downloads
5
Readme
LockX
锁住代码片段,缓存等待队列,控制并发
Install
$ npm i lockx --save
Dev
const Lock = require('lockx');
const lockName = 'test';
let index = 10;
while (index-- > 0) {
Lock.lock(lockName, { index }, async function (data) {
console.log('\n----lock----', data);
let result = void (0);
try {
result = await doSomeing();
console.log('result->',result);
} catch (error) {
console.error('error->',error);
} finally {
console.log('----unlock----\n');
Lock.unlock(lockName);
}
});
}
function doSomeing() {
return new Promise((resolve, reject) => {
let time = Math.floor(Math.random() * 10);
let red = '\033[0;31;1m';
let normal = '\033[0;38;1m';
let green = '\033[0;32;1m';
setTimeout(() => {
resolve(time < 5 ? `${green}小于5s${normal}` : `${red}大于5s${normal}`);
}, time * 500);
});
}
温馨提醒
lockx
没有设置过期清除,所以要保证lock
后一定要unlock
,强烈建议用上try/catch