How to run concurrent promises in JS and then wait for them to finish without Promise.all()?

mic*_*vka 3 javascript node.js promise

So I need to do something like:

promiseFunction1().then((result) => {

}).catch((err) => {
    // handle err
});

promiseFunction2().then((result) => {

}).catch((err) => {
    // handle err
});

....

promiseFunctionN().then((result) => {

}).catch((err) => {
    // handle err
});

// WAIT FOR BOTH PROMISES TO FINISH
functionWhenAllPromisesFinished();
Run Code Online (Sandbox Code Playgroud)

I cannot use Promise.all, as I DO NOT CARE IF ONE or ALL OF THEM FAIL. I need to be sure that ALL promises have finished. Also, the callback functions in then() are quite unique to each of the promiseFunctionX().

I am sure this is somewhat trivial, but I cannot figure it out. My initial idea was to keep a counter in the upper scope of running promises and incrementing it when one is run and decrementing it in finally(). Then I would need some async function checkIfRunningPromisesAre0() , but I am not sure how to implement this either, as it looked like recursion hell.

Here is my sample, but consider it just a material to laugh at poor implementation:

async function RunningPromisesFinished(){
    if(RunningPromises > 0){
        await sleep(2000);
        return await RunningPromisesFinished();
    }else{
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

on top of that Id have to implement async function sleep(N) and in few seconds the recursion level would be high, which im sure is not good for RAM.

Jon*_*lms 5

Collect all the promises:

  const promise1 = promiseFunction1().then((result) => {
   }).catch((err) => {
     // handle err
   });
Run Code Online (Sandbox Code Playgroud)

Then you can use Promise.all on them:

  await Promise.all([promise1, promise2, /*...*/ ]);
Run Code Online (Sandbox Code Playgroud)

I cannot use Promise.all, as I DO NOT CARE IF ONE or ALL OF THEM FAIL

For sure you can. As you added a .catch to each promise that gets the promise chain back into the resolution branch, promise1 will never reject, therefore Promise.all will never reject too.