const queueIfRunning = originalFn => { let queuedCall; let isRunning; const runner = async (...originalArgs) => { // If currently running, queue the next run if (isRunning) { queuedCall = originalArgs; } else { isRunning = true; await originalFn(...originalArgs); isRunning = false; // After the async method finishes, check if someone else is in the // queue, and if so, kick off another call if (queuedCall) { console.log('draining queue!'); const queuedArgs = queuedCall; queuedCall = null; await runner(...queuedArgs); } } }; return runner; };