Skip to content

A lockWith function (and tryLockWith)? #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
HugoMendes98 opened this issue Jun 19, 2023 · 0 comments · Fixed by #8
Closed

A lockWith function (and tryLockWith)? #2

HugoMendes98 opened this issue Jun 19, 2023 · 0 comments · Fixed by #8
Assignees
Labels
enhancement New feature or request Mutex
Milestone

Comments

@HugoMendes98
Copy link
Member

HugoMendes98 commented Jun 19, 2023

Being able to wrap the lock process inside a function could be nice.


So the following code:

async function doSomething() {
  const mutex = new Mutex();

  let data = 0;
  await mutex.lock();

  const myValue = 123;
  data = myValue * 2;

  await mutex.unlock();  

  // data === 246
}

Could be rewritten as follows:

async function doSomething() {
  const mutex = new Mutex();

  const data = await mutex.lockWith(async () => {
    const myValue = 123;
    return myValue * 2;
  })
  
  // data === 246
}
@HugoMendes98 HugoMendes98 added enhancement New feature or request Mutex labels Jun 19, 2023
@HugoMendes98 HugoMendes98 self-assigned this Jun 19, 2023
@HugoMendes98 HugoMendes98 added this to the v1.0.0 milestone Jun 21, 2023
HugoMendes98 added a commit that referenced this issue Jun 23, 2023
@HugoMendes98 HugoMendes98 linked a pull request Jun 23, 2023 that will close this issue
HugoMendes98 added a commit that referenced this issue Jun 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Mutex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant