Created
November 24, 2020 03:53
-
-
Save AndrewRayCode/a3897c4bdc5141ee6bee3e443c30a460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment