1

Consider the following situation :

// The caller 

const returnValueEWGP = await generator.GenerateEightWeeks(
  packageId
);         


// The callee 


GenerateEightWeeks = async packageId => {

   let weekPromises = [];
   // A lot of code , eventually we have array of promises "weekPromises" with data

    await Promise.all(weekPromises)
      .then(success => {

        const numberOfZeros = success.filter(x => {
          return x == 0;
        }).length;
        const numberOfOnes = success.filter(x => {
          return x == 1;
        }).length;

        console.log(
          `Before going back to Leads : Zeros : ${numberOfZeros} , Ones : ${numberOfOnes}`
        );
        return {
          zeros: numberOfZeros,
          ones: numberOfOnes
        };
      })
       .catch(failed => {            
        return {
          zeros: 1,
          ones: 0
        };
      });


}

The problem is that returnValueEWGP is always undefined.

How can I get the data back to the invoking code ?

JAN
  • 21,236
  • 66
  • 181
  • 318

2 Answers2

0

You do not return anything really try,

return await Promise.all(weekPromises)
      .then(success => {

        const numberOfZeros = success.filter(x => {
          return x == 0;
        }).length;
        const numberOfOnes = success.filter(x => {
          return x == 1;
        }).length;

        console.log(
          `Before going back to Leads : Zeros : ${numberOfZeros} , Ones : ${numberOfOnes}`
        );
        return {
          zeros: numberOfZeros,
          ones: numberOfOnes
        };
      })
       .catch(failed => {            
        return {
          zeros: 1,
          ones: 0
        };
      });
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
-1

You need to return a value from GenerateEightWeeks. You're currently returning undefined implicitly after all the promises are resolved.

Rowland
  • 1,728
  • 1
  • 12
  • 19