All Projects → mgtitimoli → await-mutex

mgtitimoli / await-mutex

Licence: other
Promised based Javascript Mutex

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to await-mutex

Asynquence
Asynchronous flow control (promises, generators, observables, CSP, etc)
Stars: ✭ 1,737 (+4725%)
Mutual labels:  promises, async-programming, async-await
apr
this is like caolan/async which is like lodash but async, but awaitful
Stars: ✭ 75 (+108.33%)
Mutual labels:  promises, async-programming, async-await
Promesa
A promise library for Clojure(Script)
Stars: ✭ 277 (+669.44%)
Mutual labels:  promises, async-programming, async-await
Swiftcoroutine
Swift coroutines for iOS, macOS and Linux.
Stars: ✭ 690 (+1816.67%)
Mutual labels:  promises, async-await
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+10397.22%)
Mutual labels:  promises, async-await
Async Javascript
Asynchronous Programming in JavaScript
Stars: ✭ 372 (+933.33%)
Mutual labels:  promises, async-await
Radon
Object oriented state management solution for front-end development.
Stars: ✭ 80 (+122.22%)
Mutual labels:  promises, async-await
Asynchronous
Implementation-agnostic asynchronous code
Stars: ✭ 13 (-63.89%)
Mutual labels:  promises, async-programming
run exclusive
⚡🔒 Wait queue for function execution 🔒 ⚡
Stars: ✭ 22 (-38.89%)
Mutual labels:  async-await, mutex
typescript-async
Creating Asynchronous Code with TypeScript
Stars: ✭ 44 (+22.22%)
Mutual labels:  promises, async-await
Async Javascript Cheatsheet
Cheatsheet for promises and async/await.
Stars: ✭ 1,902 (+5183.33%)
Mutual labels:  promises, async-await
promisify-child-process
seriously like the best async child process library
Stars: ✭ 54 (+50%)
Mutual labels:  promises, async-await
Unityfx.async
Asynchronous operations (promises) for Unity3d.
Stars: ✭ 143 (+297.22%)
Mutual labels:  async-programming, async-await
P Map
Map over promises concurrently
Stars: ✭ 639 (+1675%)
Mutual labels:  promises, async-await
Tascalate Concurrent
Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage and related extensions to java.util.concurrent.ExecutorService-s
Stars: ✭ 144 (+300%)
Mutual labels:  promises, async-programming
aioudp
Asyncio UDP server
Stars: ✭ 21 (-41.67%)
Mutual labels:  async-programming, async-await
Rx.Book
High level asynchronous programming with Reactive Extensions
Stars: ✭ 67 (+86.11%)
Mutual labels:  async-programming, async-await
doasync
Promisify functions and objects immutably
Stars: ✭ 27 (-25%)
Mutual labels:  promises
js-training
JS Training Course
Stars: ✭ 39 (+8.33%)
Mutual labels:  promises
awesome-dotnet-async
A curated list of awesome articles and resources to learning and practicing about async, threading, and channels in .Net platform. 😉
Stars: ✭ 84 (+133.33%)
Mutual labels:  async-await

await-mutex

Promised based Mutex for cases where you need to synchronize sequentially the access to a single resource from multiple locations.

A typical use case for a mutex is when multiple asynchronous processes are fired and all of them have to execute another, but the same, asynchronous process as they arrive, one at a time, waiting for previous call (if any) to finish before calling it again.

Examples

file-appender.js

Let you create an object to perform file appends one at a time.

import * as fs from "fs";
import Mutex from "await-mutex";

export default class FileAppender {

    constructor(filename) {

        this._filename = filename;
        this._mutex = new Mutex();
    }

    async append(data, options = undefined) {

        let unlock = await this._mutex.lock();

        fs.appendFile(this._filename, data, options, error => {

            unlock();

            if (error) {
                throw error;
            }
        });
    }
}

API

import Mutex from "await-mutex";

Mutex

Creates an instance of Mutex (can not be called without new).

let mutex = new Mutex();

Mutex.prototype.isLocked

Returns if the mutex instance is (true) or not locked (false).

let unlock = await mutex.lock();

console.log(mutex.isLocked()); // prints true

Mutex.prototype.lock: Promise

  • Waits until the mutex is unlocked and then locks it.
  • It returns an ES2015 standard Promise (this allows the use of async/await) which gets resolved once the mutex is unlocked.
  • The promise resolution value is an unlock function that has to be called once the mutex needs to be unlocked.
async function someFunc(mutex) {

    let unlock = await mutex.lock(); // wait until mutex is unlocked

    setTimeout(unlock, 3000);

    console.log(someFunc.name);
}

async function someOtherFunc(mutex) {

    let unlock = await mutex.lock(); // wait until mutex is unlocked

    console.log(someOtherFunc.name);
}

let mutex = new Mutex();

someFunc(mutex); // prints SomeFunc inmediately
someOtherFunc(mutex); // waits 3 secs for mutex to be unlocked and then prints SomeOtherFunc

Installation

With npm do:

npm install --save await-mutex

Contributing

Contributors

How to

Take a look to the Contributing Guide

license

Unlicense.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].