Felipe Koshino
Felipe Koshino

Reputation: 5

"How do I return just the value from the object Promise in my code bellow?

At the end of the function queensAttack() I have two async functions (processBatch and processLargeArray) that return Promises to me, I still getting the object Promise as a result of calling the function, but as an output I just want the value from the Promise object.
So how do I get rid of this unwanted "Promise { <state>: "fulfilled", <value>: 1449 }" output?

function queensAttack(n, k, r_q, c_q, obstacles) {
  var ary = [];
  var count = 0;
  var finalResult = 0;

  //Calculating the number of moves for lines without obstacles

  if (k === 0 && !obstacles) {
    count = ary[0] + ary[1] + ary[2] + ary[3] + (n - 1) * 2;
    return count;
  } else {
    var lent = obstacles.length;
    var batSize = lent / 10;

    //Declarations of accumulators vales of lines: F1, F2, D2, D1

    // --------------------------------------------------------------

    function qAttack(batch, p, lastBat) {
      //Counting the number of moves taking into account obstacles
    } //qattack6()

    // --------------------------------------------------------------

    let processBatch = async (batch, p, lastBat) => {
      return new Promise((resolve, reject) => {
        resolve(qAttack(batch, p, lastBat));
      });
    };

    let processLargeArray = async (bigAr, batchSize) => {
      const lastBatch = lent - batchSize;

      for (let i = 0; i < bigAr.length; i += batchSize) {
        const batch = bigAr.slice(i, i + batchSize);

        try {
          let results = await processBatch(batch, i, lastBatch)
          finalResult = results
        } catch (error) {
          console.error(`Error processing batch ${i / batchSize + 1}:`, error);
        }
      } //for
      return finalResult
    };

    if (lent > 1000) {
      return processLargeArray(obstacles, batSize)
    } else {
      qAttack(obstacles, 1, 1);
    }
  } //else
}

Upvotes: -2

Views: 22

Answers (0)

Related Questions