Is there such a thing as an atomic test-and-set, semaphore, or lock in Javascript?
I have javascript invoking async background processes via a custom protocol (the b
I had the same issue, here is how I solved it. It works fine for two concurrent processes. If you have three processes or more, it is possible that two processes start together.
var isRunning = false;
...
var id = setInterval(function(){ //loop until isRunning becomes false
if (!isRunning){
isRunning = true;
//do something synchronous or use a promise
isRunning = false;
clearInterval(id); // stop the loop here
}
, 10);
It is better than the while loop because it solves concurrency/async issues for reading/setting isRunning
.