DirtyHairy / async-mutex

A mutex for synchronizing async workflows in Javascript
MIT License
1.14k stars 63 forks source link

Communicate value to waiting code #60

Closed cryptofomo1 closed 2 years ago

cryptofomo1 commented 2 years ago

Hello folks,

I don't know if there is a straight link to async-mutex but i am actually doing this thing:

// listening for events 
provider.pairContract.on('Sync', async (...dexReserves) => {
    ...
    // No exclusive code here, can be // if multiple events are triggered really quickly
    ...
    // Here we should be able to be notified from the thread running the exclusive code
    var release = await mutex.acquire();
    // Exclusive code - may take several seconds, so many threads will wait for the mutex to be unlocked
    release();
}

What i'm trying to do here is that i would like to communicate a value from the thread running the exclusive code, to the waiting threads waiting for mutex to be unlocked. Why ? because conditions of execution of the waiting threads may vary depending of what happens in the thread having the lock.

I was thinking using an EventEmmiter, but i was wondering if may be there is a way to communicate a variable from a synchronized portion of code thanks to async-mutex ?

Thanks a lot and GG for this lib ! Cheers,