#Is there an idiomatic/common/modern way of creating a queue of jobs to be executed one after anot...

25 messages · Page 1 of 1 (latest)

meager cobalt
#

Is there an idiomatic/common/modern way of creating a queue of jobs to be executed one after another using Deno and typescript?

Example: Multiple users wants to execute a resource heavy job by clicking a button on their dashboard, however to not lag the host, only one job should be executed at a time.

frail swallow
#

I would create an array with queued tasks, when system ends first task, it removes it from array and then start the next one

meager cobalt
#

also how to create a global array

frail swallow
#

I would create tasks like:
[function, callback]

#

It executes the function and then calls the callback with the result of the function

meager cobalt
#

Since my task is always the same but for different conditions i can use the same function

frail swallow
#

Then maybe you could simply use:
[args, callback]

meager cobalt
#

is the callback really needed?

frail swallow
#

Depends on what you want to do with the result

meager cobalt
#

i think i don't need a callback

#

the real issue here is how to create a global variable and continuous running process that checks for the array

frail swallow
#

I think you have a globalThis object in deno

#

No sorry it's just window

#

So just use
window[my array]

#

Then create a function to execute the last queued task, and check it's running only once at a time. Each time you append something to the array, check if function is running

#

Ex:

let running = false;
function startTask() {
    if (running) return;
    running = true; task(window["example"].shift());
running = false;
if (window["example"].length) startTask(); 
}
meager cobalt
#

i understand, will try this method

#

your function has one problem we should check if there is more tasks in the queue out of the startTask() function, because executing the same function inside it can potentially never get out if there is always jobs to be done and i'm sure it would cause memory congestion or leaks at some point.

#

but i see your point

frail swallow
rustic marlin
fallen cape