A job queue in two lines of JavaScript
jameshfisher.com
jameshfisher.com
At least this stackoverflow answer suggests the handlers are GC’ed: https://stackoverflow.com/questions/79448475/are-promise-han...
The original promise needs to reference the chained promise, not the other way round.
As jobs complete I would expect the top of the chain to be eligible to be garbage collected.
a = b.then(c);
When `b` is resolved, it is garbage collected, even if a reference to `a` is retained.(If the runtime maintains async stack traces, that could be an issue...but that is a common issue, and the stack depth is limited.)
await job1()
await job2()
await job3()
await job4()
Except dynamicModern frontend development performance best practise is to allow for garbage collection only when the browser crashes.
Even if you are doing `then().then()`, something _else_ in the code base could retain the promise somehow and then your whole chain isn't GCable!
For example https://github.com/rxaviers/async-pool library that implements concurrency for async iterators (and uses promise.race) subtly creates GC pressure which you dont see until you make a whole lot of calls and suddenly its slow
It creates 1 object allocation per enqueue.
async function runTasks(tasks: Job[]) { for (let task of tasks) { try { await task() } catch (e) { } } }
chain
.then(job)
.catch((err) => {
console.error(err);
return job();
});
Otherwise failures would need to be logged by the job itself before rejecting the promise. type Job = () => Promise<unknown>;
This is not JS.`prom.then(fn)` creates a new promise. The new promise’s resolver function is the `fn` inside `then(fn)`, and the new promise’s asynchronous computation is the original promise’s resolver function.
1. Type definition 2. Chain definition 3. Enqueue definition
Transpilation does not change the fact that the input is these 3 lines of code in 2 different languages.
I wonder why are you downvoted for being as factual and neutral as it is technically possible.