Skip to content

Instantly share code, notes, and snippets.

@cefn
Created January 14, 2022 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cefn/151b6fd36ac1bc64b53613524f5b0c31 to your computer and use it in GitHub Desktop.
Save cefn/151b6fd36ac1bc64b53613524f5b0c31 to your computer and use it in GitHub Desktop.
Simple lock implementation
let releaseGlobalsPromise: null | Promise<void> = null;
async function acquireGlobalsLock() {
while (releaseGlobalsPromise) {
await releaseGlobalsPromise;
}
let release!: () => void;
releaseGlobalsPromise = new Promise(
(resolve) =>
(release = () => {
releaseGlobalsPromise = null;
resolve();
}),
);
return release;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment