skip to Main Content

I have an exec function that calls 2 other functions and await both of them before resolving, but the getData return values and the convertToArray return undefined. When I call the convertToArray function then the getData function, then the convertToArray return values, and the getData function return undefined.

The exec function:

function exec(resultset) {
  return new Promise(async (resolve, reject) => {
    try {
      var getdata = await getData(resultset);
      var convertdoarray = await convertToArray(resultset);
      resolve(convertdoarray);
    } catch (err) {
      reject(err);
    }
  });
}

The getData function:

function getData(resultset) {
  return new Promise((resolve, reject) => {
    resultset.toObjectIter((err, rs) => {
      if (err) {
        reject(err);
        return;
      }
      var rowIter = rs.rows;
      var rows = [];
      var row = rowIter.next();
      while (!row.done) {
        rows.push(row.value);
        row = rowIter.next();
      }
      rs.rows = rows;
      console.log(rows.length);
      resolve(rows);
    });
  });
}

The convertToArray function:

function convertToArray(resultset) {
  return new Promise((resolve, reject) => {
    try {
      resultset.toObjArray(function (err, results) {
        if (err) {
          reject(err);
          return err;
        } else {
          //Returning the result
          resolve(results);
        }
      });
    } catch (err) {
      reject(err);
      return err;
    }
  });
}

How i call the exec function:

exports.gues = async function () {
  return new Promise((resolve, reject) => {
    const results = await exec(resultset)
                        .then((e) => {
                          resolve(e);
                          return;
                        })
                        .catch((err) => reject(err));
 });
}

I am expecting that when both function is called in the exec function, it awaits 1 function, get the expected value then call the 2nd function, get the expected value and then return.

Also, I want when I call the exec function if the exec function takes more than 2 minutes, it returns an error.

2

Answers


  1. I think you can use return instead of resolve:

    const results=await exec(resultset)
      .then((e) => e)
      .catch((err) => console.error(err));
    
    Login or Signup to reply.
  2. Assuming use of this JSBC wrapper for node, you might only need function jdbc.resultset.prototype.toObjArray (callback) to get an array of row values. If so you could try something like

    exports.gues = function( resultset) {
       return Promise.race(
           new Promise( (resolve, reject) => 
               resultset.toObjArray( ( _, rows) => resolve(rows) )
           ),
           new Promise( (resolve, reject) =>
               setTimer( 
                   () => reject( Error("Timeout getting resultset rows")),
                   2*60*1000
               )
           )
       );
    }
    

    to get a promise for the row values with a 2 minute timeout.

    (Note marking a function that always returns a promise as aysnc is superfluous and only introduce extra processing).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search