I am trying out JS and wrapping my head around its async
model. There is a project I am building for practice. It is a simple audio calling app using webRTC.
A scenario I am having trouble understanding is given below:
User A can mute their mic via calling mute_mic()
. This function is as follows:
async function mute_mic(e) {
let credential_payload = await retrieveOwnCred();// sensitive data, get from server, don't trust client
// do other mic muting stuff
}
User B is the host of the call. So they can remove User A's mic. To do that, the host sends a signal to User B. User B's client then executes the following:
async function leave_mic() {
stopped = await stopPublishingStream(streamID);
// some other clean up
}
At times this happens: User A is muting their mic, while the host has simultaneously asked to take their mic away. I can see unpredictable stuff happening. Maybe it is because the execution of the two async
functions gets mixed up?
If that is the case, what is the best pattern to handle a situation like this? Can an industry expert provide an illustrative example?
I would want these functions to run synchronously, one after another. But I also need them to be async
, since there is an await
call within them. What tactics can I employ here?