0.0.5 • Published 6 years ago

lockx v0.0.5

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

LockX

锁住代码片段,缓存等待队列,控制并发

Build Status npm license FOSSA Status

Install

NPM

$ 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

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago