简体   繁体   中英

Async await promise pending

I am trying to create a function that adds and returns the next highest value from a mysql table row based on a search criteria. My function looks like:

    async function getNextNumber(systemType){
        let newCounter
        try{
            if(systemType == 'POS'){
                await pool.query(`Update counters set value = value + 1 where type = 'POS'`)
                let newCounter == await pool.query(`Select value from counters where type = 'POS'`)
                console.log(newCounter)
            }

            if(systemType == 'ST'){
                await pool.query(`Update counters set value = value + 1 where type = 'ST'`)
                let newCounter = await pool.query(`Select value from counters where type = 'ST'`)
            }



            return newCounter
        }
        catch(err){
            console.log(err)
        }


    }



console.log(getNextNumber('POS'))

This gives me:

Promise { pending}

[ RowDataPacket { value: 101 } ]

I wanted the [ RowDataPacket { value: 101 } ] result where it says promise pending.

I also tried:

async function test(){
    console.log(await getNextNumber('QC'))
}

This gives me no result

I see several coding errors:

  1. Only your first declaration of let newCounter should have the let in front of it. When you then assign newCounter a value, remove the let . That is creating a new locally scoped (to the local block) variable, assigning a value to it and then it goes out of scope and the top level newCounter never gets a value.

  2. Assignment is done with = , not with == unless all you want in the variable is a boolean. So change let newCounter == await pool.query(...) to just newCounter == await pool.query(...) .

  3. All async functions return a promise and you HAVE to use .then() or await on the returned promise from calling getNextNumber() . So, your second try with console.log(await getNextNumber('QC')) should work once you fix the other coding errors.

And, then one more thing to check.

Are you 100% sure that your version of your database is returning a promise from pool.query() ? await ONLY does something useful if the value you are awaiting is a promise and it contains no magic to make other types of asynchronous operations actually wait and return a value. await needs to be used with a promise.


Putting this together (and assuming you're using the right pool.query() interface that returns a promise:

async function getNextNumber(systemType) {
    let newCounter;
    try {
        if (systemType == 'POS') {
            await pool.query(`Update counters set value = value + 1 where type = 'POS'`);
            newCounter = await pool.query(`Select value from counters where type = 'POS'`);
            console.log(newCounter);
        } else if (systemType == 'ST') {
            await pool.query(`Update counters set value = value + 1 where type = 'ST'`);
            newCounter = await pool.query(`Select value from counters where type = 'ST'`);
        } else {
            // probably need some other return value or error handling here
            throw new Error("invalid systemType in getNextNumber()")
        }
        return newCounter;

    } catch (err) {
        console.log(err)
        throw err;            // need to propagate the error
    }

}

console.log(await getNextNumber('POS'));

// or with error handling
getNextNumber('POS').then(val => {
    console.log(val);
}).catch(err => {
    console.log(err);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM