skip to Main Content

Hello friends of logical thinking.

The function ttupdate will be used to update DB time tables with DOM data… and at initiation=>

The function initTimeTables will only be used during initiation, so that time table webpages will render (and eventlisteners will work) with dummy data from DB.

The dummy data is produced by an imported function "initValues" and returns an array with the length of 168.

Basically DB tables are created if they don’t exist and populated with initial data if they hold less then the full 168 indexes.

The code below works fine (!) when the function (including parameter) is called once, or several times with a certain delay.

If I call it 5 times like below, I will not get an error, but the tables will only populate with about 30% to 50% of the rows.

What am I doing wrong regarding sequentialising asynchronous methods? Is the next call not awaiting the last loop of the previous run?

async function ttupdate(timeTableName, ttFieldId, ttFieldValue) {
    let [result] = await pool.query('REPLACE INTO ' + timeTableName + ' (ttfield , ttfieldvalue , ttname) VALUES ("' + ttFieldId + '" , "' + ttFieldValue + '" , "' + timeTableName + '")');
    return result
}

async function initTimeTables(timeTableName) {
    await pool.query ('CREATE TABLE IF NOT EXISTS ' + timeTableName + ' (ttfield VARCHAR(45), ttfieldvalue VARCHAR(45), ttname VARCHAR(45), UNIQUE KEY unique_ttfield (ttfield))');
    let [tableLength] = await pool.query('SELECT ttname FROM ' + timeTableName + ' ' );
    if(tableLength.length < 168) {
        let initValuesVar = await initValues(); 
        for(i=0; i<=167; i++) { 
            let inputTimeTableName = timeTableName;       
            let inputFieldId = initValuesVar[i].ttfield;
            let inputFieldValue = initValuesVar[i].ttfieldvalue;
    
            await ttupdate(inputTimeTableName, inputFieldId, inputFieldValue);
        }
    } else {return;}
}


initTimeTables('always')
initTimeTables('never')
initTimeTables('custom1')
initTimeTables('custom2')
initTimeTables('custom3')

2

Answers


  1. It appears you are running this from a script, and that will not wait for all promises to be resolved before terminating. You don’t need to do everything sequentially, but to ensure they all complete you can do something like this:

    
    const promises = [initTimeTables('always'),
    initTimeTables('never'),
    initTimeTables('custom1'),
    initTimeTables('custom2'),
    initTimeTables('custom3')];
    
    await Promise.all(promises);
    
    
    Login or Signup to reply.
  2. You can serialize all the calls to initTimeTables() with await. You can put this inside an async IIFE.

    (async function() {
    await initTimeTables('always')
    await initTimeTables('never')
    await initTimeTables('custom1')
    await initTimeTables('custom2')
    await initTimeTables('custom3')
    })();
    

    This can also be written as a series of .then():

    initTimeTables('.always')
        .then(initTimeTables('never'))
        .then(initTimeTables('custom1'))
        .then(initTimeTables('custom2'))
        .then(initTimeTables('custom3'))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search