···44 let timer = null;
55 let lastArgs = null;
6677+ let throttleId = 0;
88+79 function throttled(...args) {
1010+ // always make sure our callback args are fresh
811 lastArgs = args;
99- if (timer === null) {
1010- timer = setTimeout(async () => {
1111- followUp(await callback(...lastArgs));
1212- timer = null;
1313- }, minT);
1212+1313+ // early exit if we're waiting for the throttle still
1414+ if (timer !== null) {
1515+ return;
1416 }
1717+1818+ // otherwise we're starting a timer. make sure we know who we are.
1919+ let myThrottle = ++throttleId;
2020+ // we immediately get a new timer id
2121+ timer = setTimeout(async () => {
2222+ // make sure we synchronously clear this so next throttle isn't lost
2323+ timer = null;
2424+ // *then* we can start the actual callback
2525+ let res = await callback(...lastArgs);
2626+ // but since we awaited the callback, it's now possible to be stale, so check
2727+ if (myThrottle === throttleId) {
2828+ followUp(res);
2929+ }
3030+ }, minT);
3131+1532 }
1633 return throttled;
1734}