I am using meteor. I have a route defined in iron router as follows,
Router.route('/api/generatereceipt', {where: 'server'}).post(function(req, res, next) {
console.log("API: generatereceipt invoked.");
const reqBody = req.body;
....
}
I want to generate a receipt one at a time. i.e. the receipt needs to have a unique number which is incremental in nature.
Right now I'm reading the previously stored receipt number and incrementing it by one. I consider that as the receipt number for currently processing receipt.
However, This works fine as long as the generatereceipt API is not called concurrently by multiple clients. When the API is called simultaneously, the same receipt number is considered for all the concurrently processed receipts.
Therefore I want to make a request check if the same REST API is called by some other client in some other request is under process. If it is in the process then I want to wait till its execution in that request thread finishes.
There should be no issue with concurrent requests. See the following example:
as you can see, the calls will resolve to the incremented ids: