node.jspromisepromise.all

Pushing promises in an array the array remains null


I am pushing the promises in promises =[] array but when passing the promises in promises. All the array remains null which resolves and send null result if my code has any issue please answer

searchProductVariant: (start, end, combinations) => {
return new Promise((resolve,reject)=>
{
  var promises = [];
  console.log(start, "  ", end);
  for (let i = start; i < start + end; i++) {
    vv = combinations[i - start].product_variant_name.split("_");
    console.log("kk", vv);

    vv.forEach((v) => {
      sql = `SELECT id FROM ecommerce.variant_values as varval where varval.value like '${v}'`;
      pool.query(sql, [], (error, results, fields) => {
        if (error) {
          console.log("the errrror is", error);
        }
        if (results[0].id) {
          console.log("i is ", i);
          let temp = addProductVariantDetails(i, results[0].id);
          promises.push(temp
          );
        }
      });
    });
  }
  console.log(promises); //it prints null
  return Promise.all(promises)
    .then((resul) => {
      return resolve(resul);
    })
    .catch((err) => {
      return reject(err);
    });
})
}

Solution

  • pool.query receives a callback, and that's the only thing that adds to the promises array. I think it's likely that you're just trying to run Promise.all before any of the query callbacks return. You might need to switch to a Promise-based version of pool.query, if one exists, or you can write your own like this.

    searchProductVariant: (start, end, combinations) => {
      // DELETE: Original Promise wrapper. Return Promise.all result directly.
      var queries = [];  // NEW: Track queries.
      var promises = [];
      console.log(start, "  ", end);
      for (let i = start; i < start + end; i++) {
        vv = combinations[i - start].product_variant_name.split("_");
        console.log("kk", vv);
    
        vv.forEach((v) => {
          sql = `SELECT id FROM ecommerce.variant_values as varval where varval.value like '${v}'`;
          // NEW: Wrap pool.query to create a promise that waits for the possible add
          // to the promises array, adding the separate promises array.
          queries.push(
            new Promise((resolve, _) => {
              pool.query(sql, [], (error, results, fields) => {
                resolve([error, results, fields]);
              })
            }).then([error, results, fields] => {
              if (error) {
                console.log("the error is", error);
              }
              if (results[0].id) {
                console.log("i is ", i);
                let temp = addProductVariantDetails(i, results[0].id);
                // Keep temp in an array so we only return valid results,
                // but also return it so the returned Promise surfaces
                // any rejection issued at any time.
                promises.push(temp);
                return temp;
              }
            }));
        });
      }
      console.log(promises);
      // Wait for the queries, then wait for the promise results.
      return Promise.all(queries).then(() => Promise.all(promises));
    }
    

    You could also simplify this code by dropping the promises array entirely and simply returning Promise.all(queries) at the end; however, you'd need to filter out the undefined results that come from any queries promises that result in errors or ID-less results, and I don't know enough about your code to know whether undefined can be properly filtered. (You also don't check that results.length > 0, but as you have it that would result in a Promise rejection either way.)