Socket
Book a DemoInstallSign in
Socket

mutex-js

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mutex-js

provides a promise-based mechanism for locking around code which requires synchronization

1.1.5
latest
Source
npmnpm
Version published
Weekly downloads
93
-40.38%
Maintainers
1
Weekly downloads
 
Created
Source

mutex-js

Provides a promise-based mechanism for locking around code which requires synchronization (i.e. you may want to synchronize functions that span multiple iterations of the event loop).

Install

npm install --save mutex-js

Getting started

const Mutex = requires('mutex-js');

const mutex = new Mutex();
mutex.lock(() => {...});

Example

Below, we call an async function run several times with different args (the calls will run in parallel). The function reads data from a file, appends the input to the data and finally writes the modified data back to the file. Without synchronization, each call to run may overlap the results of the previous one. Wrapping the function implementation in a locking mechanism ensures that only one call is executing the critical section at a time.

const mutex = new Mutex();

// reads file, returns a promise with file data
const readFromFile = () => {...}
// appends arg to data, returns a promise with modified data
const appendData = (data, arg) => {...}
// writes data to file, returns a promise
const writeToFile = (data) => {...}

const run = (arg) =>
  mutex.lock(
    readFromFile()
    .then(data => appendData(data, arg))
    .then(writeToFile)
  );

Promise.all([
  run('foo'),
  run('bar')
])
.then(...)
.catch(...)

Keywords

mutex-js

FAQs

Package last updated on 23 Dec 2016

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.