8000 call of unlock() is too convoluted · Issue #9 · dominictarr/lock · GitHub
[go: up one dir, main page]

Skip to content

call of unlock() is too convoluted #9

Open

Description

@ceresith

lock('key', function(unlock) { unlock()(); })

Currently, this is too convoluted. It should be:

lock('key', function(unlock) { unlock(); })

Activity

  1. dominictarr commented on May 2, 2016

    @dominictarr
    Owner

    your example code is misleading because you are not doing anything that is async.

    when you use it as a callback, as given in the examples, it is not as bad.

    lock(key, function(unlock) {
      fs.readFile(key, unlock())
    })
    
  2. alflennik commented on Apr 9, 2018

    @alflennik

    In the age of async / await this feels a little odd. I had to do this:

    await new Promise((resolve, reject) => {
      lock('key', async unlock => {
        await asyncFunction()
        unlock()();
        resolve();
      });
    });

    It would be awesome if the callback could await a promise before releasing. Then I could do this:

      await lock('key', async () => {
        await asyncFunction()
      });
  3. poislagarde commented on May 30, 2018

    @poislagarde

    @alflennik we promisify it like this:

    function promisifiedLock () {
      const lock = Lock()
      return (key) =>
        new Promise((resolve) => {
          lock(key, (release) => {
            resolve(promisifyRelease(release))
          })
        })
    }
    
    function promisifyRelease (release) {
      return () =>
        new Promise((resolve, reject) => {
          release((err) => {
            if (err) {
              reject(err)
            } else {
              resolve()
            }
          })()
        })
    }

    then use it like this:

    const lock = promisifiedLock()
    
    const release = await lock('key')
    try {
      await asyncFunction()
    } finally {
      await release()
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0