In Javascript, how to control data contention if the event listener is asynchronous?

consider the following code

whatever.onclick = async () => {
    const a = await(await fetch("step-1")).text();
    const b = await(await fetch("step-2")).text();
    whatever.textContent = a + b;
}
The

event listener is asynchronous, so if the user clicks again between step-1 and step-2 , there will be competition. Remote is a third party, so it can"t be controlled.

now there are several possible requirements:

  • discard the new request if the previous request has not been completed; or
  • if the previous request has not been completed, the new request should be returned with the old request; or
  • if the previous request has not been completed, the new request should be queued.

do you have a suitable wheel, or do you have to build it yourself?

feels that this is a very reasonable requirement. Why doesn"t loadsh have

?
Mar.19,2021

set a time limit for async. Click, that is triggered repeatedly in a short period of time is actually executed only once.

Menu