Giter Site home page Giter Site logo

await-lock's Introduction

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();
  }
});

await-lock's People

Contributors

clemens-e avatar dependabot[bot] avatar ide avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

await-lock's Issues

AwaitLock is not a constructor when importing in Typescript

This is similar to #5 but I'm not using require but Typescript with moduleResolution: "node". https://github.com/kasbah/await-lock-bug-repro

git clone https://github.com/kasbah/await-lock-bug-repro && cd await-lock-bug-repro
yarn
yarn tsc
node example.js
TypeError: AwaitLock is not a constructor
    at file:///home/kaspar/projects/await-lock/repro/example.js:2:14
    at ModuleJob.run (node:internal/modules/esm/module_job:192:25)
    at async DefaultModuleLoader.import (node:internal/modules/esm/loader:228:24)
    at async loadESM (node:internal/process/esm_loader:40:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12)

implement timeout and maxOccupationTime

similar to async-lock library, please implement timeout and maxOccupationTime

timeout -> timed out error will be returned here if lock not acquired in given time
maxOccupationTime -> occupation time exceeded error will be returned here if job not completed in given time

utility function `.acquireWhile`

I end up implementing this utility function when using this library a lot, so I thought id make a suggestion.
To make sure I don't forget unlocking, and to make it more visually apparent, I scope the lock logic in an async callback.

const lock = new AwaitLock()
await lock.acquireWhile(async () => {
    //do things with the guarded data
})

Basically aquireWhile would:
1: aquireAsync
2: await callback with try finally wrapper
3: unlock in finally

Alternate potential function names:

  • acquireDuring
  • access
  • acquireAwait

TypeError: AwaitLock is not a constructor

With the new version 1.2.0 this piece of code throws the error
TypeError: AwaitLock is not a constructor

const AwaitLock = require('await-lock');
const lock = new AwaitLock();

This works with 1.1.3 version.

Comment about "finally" not running in README example

In the README, there is the following comment in the second example:

// IMPORTANT: Do not return from here since the "finally" clause will not run!

Are you sure about that? I did some tests and I have no problems, no matter how we exit the "try" block (return or throw), the "finally" clause is always executed.

Option to return immediately if lock is taken

I have a workflow, which would benefit if it were possible to try to get the lock and fail immediately if it is taken by other greenlet.

Would you take a patch implementing this?

There are 2 ways to implement this:

  1. return Promise<bool> (true = success, false = failure)
  2. throw exception on failure, leave everything as-is on success

Support for running this from a web page

Is this module intended to be used on the web platform, or is it node only? By glancing through the code, it appears that the only node specific bit is the call to assert. Would you consider removing it for the benefit of making it possible to use this on the web?

Is order of acquiring the lock guaranteed?

This isn't an issue so much as a question, but I don't see another place to ask a question and I don't see an answer to this in the documentation.

If I have my lock object, const lock = new AwaitLock();, and I'm calling await lock.acquireAsync(); in various places in the code, will those promises that I get back from .acquireAsync() always resolve in the same order I called them?

Clarify usage

The usage example in readme doesn't give all information. Reading the code there are a couple of additional important things that would be useful to know:

  1. acquireAsync can be used with a timeout. very useful but not documented.
  2. in that case, it will reject if the timeout expires. so it should be used inside a try/catch.
  3. if a lock is not acquired, and we try to release it, it will throw an error.

I propose to create an additional example showing usage with timeout, given that there is need for a couple of additional try/catch

Source map resolution failure

I see the following warning/error at build time when using await-lock in my project:

WARNING in ./node_modules/await-lock/build/AwaitLock.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from 'D:\src\MyProject\node_modules\await-lock\src\AwaitLock.ts' file: Error: ENOENT: no such file or directory, open 'D:\src\gui\MyProject\node_modules\await-lock\src\AwaitLock.ts'

It's a TypeScript project based on Create React App (preconfigured WebPack); package.json contains:

{
    "dependencies": {
        "await-lock": "^2.2.1"
        "react-scripts": "^5.0.0",
        "typescript": "~4.5.4",
        // ...
    }
    // ...
}

I note that the node package consists of:

image

and AwaitLock.js.map contains (reformatted):

  "file": "AwaitLock.js",
  "sourceRoot": "",
  "sources": [
    "../src/AwaitLock.ts"
  ],

If I understand it correctly the error occurs because the source map refers to a file AwaitLock.ts which is not included in the package. Assuming that is the case please could you include it, to eliminate the dangling reference?

Thanks for sharing this useful library. Regards, Matt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.