The correct way to implement a critical section in NodeJs

I have an operation / task that I need to run that fires when the event fires (I don't think this last thing is really important).

The fact is that this task consists of several io operations, mainly network calls. In addition, I would like to start this task atomically, start from the end, one at a time, new tasks should not be launched until the current one ends.

I usually do this using a critical section of some kind, but I don't think there is such a thing in js or in the node database. How do you suggest me handle this case?

thank

EDIT: I saw that “critical sections are not needed, this is a single-threaded” view several times in different posts, and I think this is partly true, it only applies to synchronous actions.

Suppose for a typical scenario that uses critical sections, you need to do 2 things. A) verify the correctness of the condition; B) apply the action only if A is either true or false; action that will flip the state. You do not want 2 threads to conclude that A is false at the same time and that B must be executed, so you end A and B in the critical section to make them atomic. In node.js, if A is synchronous, then you are fine, no other thread will be started, and you can safely make B. But if A is asynchronous, before it invokes callbacks, another event for A may appear in the event queue before the first gets it B.

+4
1

Node , . Control Flow (Async, Bluebird, Q).

mscdex , , Async queue(), . , " " concurrency: 1 .

+1

All Articles