Socket
Book a DemoInstallSign in
Socket

lockx

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lockx

[新API]锁住你的代码片段,缓存等待队列,控制并发

0.0.5
latest
Source
npmnpm
Version published
Weekly downloads
6
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

queue

FAQs

Package last updated on 11 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.