2
I am experiencing difficulties in implementing an asynchronous counter using Node.js and Mongodb.
Follows the code:
app.get('/', req, res ()=> {
var number = await Number.find({}); // Leitura do valor
number++; // Incrementação do valor
await Number.save(number); // Salva valor incrementado
res.send(number); // Retorna valor para usuário
})
The question I have is this: It would be necessary to implement a semaphore to accomplish this task without running condition or Node.js handles it automatically?
It would be what kind of race condition and what objects involved?
– Augusto Vasques
This could occur when two or more users access the Uri at the same time.
– victor sousa
Nodejs is single thread. Requests are queued, even if two users make the same request simultaneously first it resolves a request and only after its resolution it passes to another user’s request.
– Augusto Vasques