I have a HTTP-triggered function which returns an increment number every time the endpoint is called. The code looks like this:
export const reserve = functions.https.onRequest((req, resp) => {
cors(req, resp, async () => {
if (req.method.toLowerCase() !== 'post') {
resp.status(405);
resp.end();
} else {
const path = `counter`;
const ref = firebase.database().ref(path);
const oldCount = (await ref.once('value')).val();
await ref.set(oldCount + 1);
resp.status(200).send({
number: oldCount
});
resp.end();
}
});
});
The problem is, if 2 invocations are very close to each other, is there a chance that the function can return the same number? If so, is there a way to prevent this?