Created
October 31, 2025 19:12
-
-
Save samukasmk/6de23621633ed213ea780400c47cec3e to your computer and use it in GitHub Desktop.
[javascript] Sample of runner queue to ensure which async function will run non parallel just after another
This file contains hidden or 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
| // Helper: wait for a given number of milliseconds | |
| function wait(ms) { | |
| return new Promise(resolve => setTimeout(resolve, ms)); | |
| } | |
| // Simple FIFO queue with O(1) enqueue/dequeue (amortized) | |
| class Queue { | |
| constructor() { | |
| this.items = []; | |
| this.head = 0; | |
| } | |
| enqueue(item) { | |
| this.items.push(item); | |
| } | |
| dequeue() { | |
| if (this.head >= this.items.length) return null; | |
| const item = this.items[this.head++]; | |
| // optional: cleanup memory when too many items were dequeued | |
| if (this.head > 50 && this.head * 2 > this.items.length) { | |
| this.items = this.items.slice(this.head); | |
| this.head = 0; | |
| } | |
| return item; | |
| } | |
| get length() { | |
| return this.items.length - this.head; | |
| } | |
| } | |
| // Create a serialized version of an async function. | |
| // Ensures only one call runs at a time, with a delay between each call. | |
| function serialize(fn, intervalMs = 3000) { | |
| const queue = new Queue(); | |
| let isProcessing = false; | |
| async function processQueue() { | |
| if (isProcessing) return; | |
| isProcessing = true; | |
| while (queue.length > 0) { | |
| const { args, resolve, reject } = queue.dequeue(); | |
| try { | |
| const result = await fn(...args); | |
| resolve(result); | |
| } catch (error) { | |
| reject(error); | |
| } | |
| await wait(intervalMs); | |
| } | |
| isProcessing = false; | |
| } | |
| // Returned function automatically enqueues calls | |
| return function (...args) { | |
| return new Promise((resolve, reject) => { | |
| queue.enqueue({ args, resolve, reject }); | |
| processQueue(); | |
| }); | |
| }; | |
| } | |
| async function slowFunction(data) { | |
| console.log(data, 'started', 'at', new Date().toISOString()); | |
| await wait(1000); | |
| console.log(data, 'finished', 'at', new Date().toISOString()); | |
| return data; | |
| } | |
| // Example usage: | |
| const queuedFunction = serialize(slowFunction, 3000); | |
| queuedFunction("a"); | |
| queuedFunction("b"); | |
| queuedFunction("c"); | |
| queuedFunction("d"); | |
| queuedFunction("e"); | |
| queuedFunction("f"); | |
| queuedFunction("g"); | |
| queuedFunction("h"); | |
| queuedFunction("i"); | |
| // a finished at 2025-10-31T19:07:25.249Z | |
| // b started at 2025-10-31T19:07:28.250Z | |
| // b finished at 2025-10-31T19:07:29.251Z | |
| // c started at 2025-10-31T19:07:32.251Z | |
| // c finished at 2025-10-31T19:07:33.252Z | |
| // d started at 2025-10-31T19:07:36.252Z | |
| // d finished at 2025-10-31T19:07:37.253Z | |
| // e started at 2025-10-31T19:07:40.253Z | |
| // e finished at 2025-10-31T19:07:41.253Z | |
| // f started at 2025-10-31T19:07:44.254Z | |
| // f finished at 2025-10-31T19:07:45.254Z | |
| // g started at 2025-10-31T19:07:48.255Z | |
| // g finished at 2025-10-31T19:07:49.256Z | |
| // h started at 2025-10-31T19:07:52.256Z | |
| // h finished at 2025-10-31T19:07:53.257Z | |
| // i started at 2025-10-31T19:07:56.408Z | |
| // i finished at 2025-10-31T19:07:57.408Z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment