0

I'm trying to collect all values from a mysql table with all the values of the referenced_table_name for each index of the table. How avoid set a random time out while waiting for a promise

To collect the expected information i need to set a random time out, otherwise my object is undefined...

module.exports = {
    getTable: async (req, res) => {
        const tablename = req.params.table,
              dbName = req.params.dbName;                
        let jsonResult = {};
        getTableValues(dbName, tablename)
            .then(tableValues => {
                getTableIndexedCol(dbName, tablename)
                    .then(indexedColumns => {
                        let indexedArr = {};
                        for (let index = 0; index < indexedColumns.length; index++) {   
                            const element         = indexedColumns[index],
                                  column          = element.column_name,
                                  referencedTable = element.referenced_table_name;
                            let allValuesRefTable = new Array();
                            getTableValues(dbName, referencedTable)
                                .then(referencedTableValues => {
                                    for (let i = 0; i < referencedTableValues.length; i++) {
                                        const el = referencedTableValues[i];
                                        allValuesRefTable.push(el.name);
                                    }
                                })
                                .catch(err => console.log(err));
/*IF NO TIMEOUT DOESN'T WORK*/
                            setTimeout(function(){
                                indexedArr[column] = allValuesRefTable;
                            }, 100);

                        }
                    setTimeout(function(){
                        jsonResult = {
                            name: tablename,
                            rows : tableValues, 
                            rowIndexed : indexedArr
                        }
                        res.json(jsonResult);
                    }, 5000);

                })
                .catch(err => console.log(err));
            })
           .catch(err => console.log(err));
    }
};

Is there a way to don't use setTimeout? or how can I 'wait' that the promise is resolved?

Here is my function getTableIndexedCol for example:

async function getTableIndexedCol(dbName, tablename) {
   const sqlRefTable = SELECT...; 
   return new Promise (async function(resolve, reject){ 
   try{
      [refTable, refTableFields] = await promisePool.query(sqlRefTable) 
   }
   catch(err){ 
      reject(err) 
   } 
   setTimeout(function () {
      resolve(refTable); 
   }, 500); 
})
Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
Ben
  • 63
  • 2
  • 9
  • See also [Set a timeout on a promise in Node](https://stackoverflow.com/questions/32461271/nodejs-timeout-a-promise-if-failed-to-complete-in-time). – tadman Mar 26 '19 at 01:12
  • you can use `async/await` or `Deferred` e.g -- `return new Promise(function(resolve, reject) { doSomething(function cb(good) { if (good) resolve(); else reject(); }); }); ` Ref: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred – Azeem Aslam Mar 26 '19 at 03:01
  • I think i must mistaken somewhere...I'm using async/await already but for this particular request i need to add extra timeout in my module.export and i don't understand why. Here is my function **getTableIndexedCol** for example: – Ben Mar 26 '19 at 04:05
  • `async function getTableIndexedCol(dbName, tablename) { const sqlRefTable = `SELECT...`; return new Promise (async function(resolve, reject){ try{ [refTable, refTableFields] = await promisePool.query(sqlRefTable) } catch(err){ reject(err) } setTimeout(function () { resolve(refTable); }, 500); })` – Ben Mar 26 '19 at 04:06
  • @Ben, add this code to the question buddy... and state in comments that you have added it. Don't paste code as comments. I've added it to the question. – Harshith Rai Mar 26 '19 at 05:41
  • 1
    @Rai, thanks. I've added the function example in the body question – Ben Mar 26 '19 at 07:40

1 Answers1

0

If you are already using async/await you can use it all the way and avoid the "Promise Hell" (nested .then calls):

module.exports = {
    getTable: async (req, res) => {
        try {
            const tablename = req.params.table,
                dbName = req.params.dbName;

            const tableValues = await getTableValues(dbName, tablename);
            const indexedColumns = await getTableIndexedCol(dbName, tablename);

            let indexedArr = {};
            for (let index = 0; index < indexedColumns.length; index++) {
                const element = indexedColumns[index],
                    column = element.column_name,
                    referencedTable = element.referenced_table_name;
                let allValuesRefTable = new Array();
                const referencedTableValues = await getTableValues(dbName, referencedTable);
                for (let i = 0; i < referencedTableValues.length; i++) {
                    const el = referencedTableValues[i];
                    allValuesRefTable.push(el.name);
                }
                indexedArr[column] = allValuesRefTable;
            }

            const = jsonResult = {
                name: tablename,
                rows: tableValues,
                rowIndexed: indexedArr
            }
            res.json(jsonResult);
        } catch (err) {
            console.log(err);
        }
    }
};
OzW
  • 848
  • 1
  • 11
  • 24
  • Okay I see, I though I needed to call it again to handle errors... Thanks, it look cleaner like this and working without extra timeout. – Ben Mar 28 '19 at 00:47