0

So I have a bunch of promises I'm trying to batch complete like so:

let promises = [];
let fixedValues = [];
let shouldIQuit = false;
let value = {
    b: 1
};

promises.push(
   new Promise(async (resolve, reject) => {
      const shouldquit = await shouldIQuit(shouldIQuitValue);

      if(shouldquit === true) {
          const fixedValue = await Values.fixThisValue(value);

          if(!fixedValue.a) {
            resolve();
           }

         fixedValues.push(fixedValue);
      }

    resolve();
});

await Promise.all(promises);

the other functions can do anything, but I need both to be awaited. It should first call the shouldIquit function, check that value, then await the result of fixThisValue and then determine if that number should be put into the array or not, and I need to do this all in parallel.

However, I'm noticing that sometimes when fixedValue comes back without fixedValue.a and it successfully hits that resolve(); in the if, it still makes it down to push the fixed value into the array fixedValues when it shouldn't. I'm thinking it's the async in the promise creating a problem. Any help with that is much appreciated.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
  • 1
    does `if(!fixedValue.a) { resolve(); } fixedValues.push(fixedValue);` need to be `if(!fixedValue.a) { resolve(); } else { fixedValues.push(fixedValue);}` – Mark Schultheiss Aug 24 '23 at 20:27
  • 2
    `resolve` is not equivalent to `return`. There is no flow control associated with resolving a promise. Execution continues to the next statement after you call the `resolve()` function. If you want you can `return resolve();` to exit your function after resolving the promise since you don't seem to be using the return value anyway. – Wyck Aug 24 '23 at 20:28
  • [Never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572)! – Bergi Aug 24 '23 at 21:47

3 Answers3

3

when fixedValue comes back without fixedValue.a and it successfully hits that resolve(); in the if, it still makes it down to push the fixed value into the array fixedValues when it shouldn't

I think you're hoping that resolve() works as a return statement (exiting the function).

         const fixedValue = await Values.fixThisValue(value);

          if(!fixedValue.a) {
            resolve();
           }

         fixedValues.push(fixedValue);

resolve() doesn't exit the function. You could update your code to do an early return

if(!fixedValue.a) {
  resolve();
  return;
}
            
fixedValues.push(fixedValue);

or you could add an else block


if(!fixedValue.a) {
  resolve();
} else {
  fixedValues.push(fixedValue);
}
alexanderbird
  • 3,847
  • 1
  • 26
  • 35
0

You need to return resolve() otherwise the code will keep running past the 'if'.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
Nulla
  • 62
  • 9
0

You need to add a return after resolve or otherwise rearrange the flow of your code.

promises.push(
   new Promise(async (resolve, reject) => {
      const shouldquit = await shouldIQuit(shouldIQuitValue);

      if ( shouldquit ) {
         const fixedValue = await Values.fixThisValue(value);
         if ( fixedValue.a ) {
            fixedValues.push( fixedValue );
         }
      }

      resolve();
   }
);

Alternate take:

promises.push(
   new Promise(async (resolve, reject) => {
      const shouldquit = await shouldIQuit(shouldIQuitValue);

      if ( shouldquit ) {
         const fixedValue = await Values.fixThisValue(value);
         if ( fixedValue.a ) {
            resolve( fixedValue );
         }
      }

      resolve( undefined );
   }
);

let fixedValues = ( await Promise.all(promises) ).filter( _ => _ !== undefined  );
ikegami
  • 367,544
  • 15
  • 269
  • 518