Skip to content
forked from ide/await-lock

Mutex locks for async functions

License

Notifications You must be signed in to change notification settings

kasbah/await-lock

 
 

Repository files navigation

AwaitLock tests codecov

Mutex locks for async functions

API

API documentation

Usage

import AwaitLock from 'await-lock';

let lock = new AwaitLock();

async function runSerialTaskAsync() {
  await lock.acquireAsync();
  try {
    // IMPORTANT: Do not return a promise from here because the finally clause
    // may run before the promise settles, and the catch clause will not run if
    // the promise is rejected
  } finally {
    lock.release();
  }
}

You can also use AwaitLock with co and generator functions.

import AwaitLock from 'await-lock';

let runSerialTaskAsync = co.wrap(function*() {
  yield lock.acquireAsync();
  try {
    // Run async code in the critical section
  } finally {
    lock.release();
  }
});

About

Mutex locks for async functions

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 96.1%
  • JavaScript 3.9%