I have one object with different properties in nodejs, there are different async function which access and modify that object with some complex execution. A single async function may have internal callbacks (or async functions), that may take some time to execute and then that function will modify that object. I want to lock that object until I'm done with all modification, only after that any other async function will access it.
Example:
var machineList = {};
function operation1() {
waitForMachineList();
//complex logic
//modification of machineList, some closure and callbacks functions and again modification of machineList
leaveFromMachineList();
}
function operation2() {
waitForMachineList();
//complex logic
//modification of machineList, some closure and callbacks functions and again modification of machineList
leaveFromMachineList();
}
function operation3() {
waitForMachineList();
//complex logic
//modification of machineList, some closure and callbacks functions and again modification of machineList
leaveFromMachineList();
}
function operation4() {
waitForMachineList();
//complex logic
//modification of machineList, some closure and callbacks functions and again modification of machineList
leaveFromMachineList();
}
Suppose machineList
is one complex object, and there are different operations are done on this object by different async methods (operation1()
, operation2()
, ...) to modify it. Those operation are called in any sequence and any number of time as per request comes from client. Each request will execute single operation.
There are some internal closure functions and callbacks (or async functions) in each operation function, it may take some time. But I want to lock machineList
object till any single operation is done.
On start of any operation, I want to lock object like waitForMachineList()
and will release lock after leaveFromMachineList()
.
So finally I want to implement locking mechanism in nodejs. Just like Critical Session in C++ and lock in C#.
So please some will help to implement it in nodejs? or suggest me any node module which I can use for this.