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

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3e637-4daa9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3e637-4daa9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?