3

I am writing a function that should execute all async functions as fast as possible, however, only 5 of them can run at the same time.

I wanted to do it using Promise.race so the implementation is not the best. The problem is that the code execution doesn't stop at await. I would expect, that counter variable would be decremented only when the promise resolves but it doesn't wait for the promise to resolve. The code doesn't stop at Promise.all either which makes me thing that my understanding of promises is not proper. The code is below and the function in question is runPromises.

async function runPromises(promises, limit) {
  const LIMIT = limit || 10;
  let promsCopy = promises.slice();
  let counter = 0;
  let queue = [];
  while (promsCopy.length !== 0) {
    if (counter < LIMIT) {
      let prom = promsCopy.shift();
      if (prom) {
        queue.push(prom());
        counter += 1;
        if (counter >= LIMIT) {
          let [completed] = await Promise.race(
            queue.map((p) => p.then((_) => [p]))
          );
          queue.splice(queue.indexOf(completed), 1);
          counter -= 1;
        }
      }
    }
  }
  await Promise.all(queue);
}

// the code below is just for testing

const asyncFuncLong = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, 6000);
  });
};

const asyncFuncShort = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, 1000);
  });
};

let fns = [
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
];

let start = Date.now();

runPromises(fns, 10).then(() => {
  console.log(Date.now() - start);
});


Edit: Fixed. Everything works now. Thank you both!

SrdjaNo1
  • 755
  • 3
  • 8
  • 18
  • [Never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572)! – Bergi Sep 16 '21 at 19:40
  • I can't use `await` syntax then I believe? – SrdjaNo1 Sep 16 '21 at 19:48
  • Why are you even using `new Promise` here? You can and should use `await` right inside of `runPromises` which is an `async` function already. – Bergi Sep 16 '21 at 19:55
  • I've fixed it now, but ran into a different issue where only first promise is resolved. – SrdjaNo1 Sep 16 '21 at 20:16
  • I think I misunderstood the intent a little bit. If you do go with `let promises = fns.map((f) => f());`, then that "starts" all the promises at once. So they are all waiting for 6 seconds, but simultaneously. In order to start them one after another, you do need to leave them as functions, then call them later on. – nullromo Sep 16 '21 at 21:50
  • Thank you. The code now works as expected. – SrdjaNo1 Sep 16 '21 at 22:04
  • Possible duplicate of [Limited parallelism with async/await in ES8](https://stackoverflow.com/a/39197252/1048572) – Bergi Sep 16 '21 at 22:29

1 Answers1

1

The fns array is a list of functions. Those functions return Promise objects. You are then passing that list of functions to your runPromises function, which appears to want to take a list of Promise objects as an argument. But you are passing it a list of functions.

My suggestions are either

  • change the runPromises function so that it actually calls prom() at some point (or just use a map, like const r = await Promise.all(queue.map((fn) => { return fn(); }));), or

  • change the definition of fns to something like let fns = [ asyncFuncLong(), asyncFuncShort(), asyncFuncLong(), ... ];.

nullromo
  • 2,165
  • 2
  • 18
  • 39
  • The second suggestion does not work, as that runs all the calls immediately. – Bergi Sep 16 '21 at 19:41
  • The functions immediately return Promise objects, which represent asynchronous tasks that have not necessarily finished executing. – nullromo Sep 16 '21 at 21:36
  • 1
    Yes, but if you do that all you can do is pass the array of promises to `Promise.all`, there is no limiting of concurrent tasks. – Bergi Sep 16 '21 at 21:53
  • Yes, that's right. I slightly misunderstood the intent of the program. I see what you mean; essentially it starts all the timers at once, which is not what was intended. It does allow you to wait for all the promises to complete, just not quite in the right way. – nullromo Sep 16 '21 at 22:07