How to implement Asynchronous Lock in NodeJs

problem description

there is a function of api: the information currently submitted is b and the database already has an information, that is, the specified task c (an and b are triggered regardless of order, and c is triggered as long as the combination exists.
the database used is mongodb

the environmental background of the problems and what methods you have tried

but the processing of this api request is time-consuming. If both an and b request api at the same time, b does not find an api or b in the logical judgment, and then everyone bypasses the judgment condition and enters the database at the same time, without triggering the specified task c

.

what result do you expect?

how to handle an and b requests during time-consuming asynchrony, such as adding program locks and queue attempts?

I hope you can give me some advice on the specific technical proposal. Thank you.

Nov.04,2021

it should be possible to use the atomic operation findAndModify of mongodb
new to false to read the modified data to determine whether it meets the conditions


insert data into An and B and write an asynchronous callback function to determine whether An and B have completed the data insertion and then execute C

.
Menu