npm i @minsize/mutex
yarn add @minsize/mutex
import { Mutex } from '@minsize/mutex'
const mutex = Mutex({
globalLimit: 1 // Global limit on how many functions a mutex can process at once
})
const fn = async () => {
const release = await mutex.wait();
try {
// ...
} finally {
release();
// or
mutex.release();
}
}
import { Mutex } from '@minsize/mutex'
const mutex = Mutex({
globalLimit: 1 // Global limit on how many functions a mutex can process at once
})
const fn = async (userId) => {
const release = await mutex.wait({
key: userId, // Whom Mutex will be used
limit: 1 // Works the same as the global limit, but only for 1 key
});
try {
// ...
} finally {
release();
// or
mutex.release({ key: userId });
}
}