79577340

Date: 2025-04-16 14:02:52
Score: 1
Natty:
Report link

Here's a Typescript version of @Heniker's answer with perhaps better naming.

function pushToExecQueue<T>(fn: (...args: any[]) => Promise<T>): (...args: any[]) => Promise<T> {
  let inprogressPromise = <Promise<T>>Promise.resolve();
  return (...args) => {
    inprogressPromise = inprogressPromise.then(() => fn(...args));
    return inprogressPromise;
  }
}

And perhaps a somewhat cleaner/clearer way of using it is

pushToExecQueue(myAsyncFunction)("Hi", "my second parameter", etc);
Reasons:
  • Has code block (-0.5):
  • User mentioned (1): @Heniker's
  • Low reputation (0.5):
Posted by: Nathan Dolan