javascript - 异步等待 promise 待处理

标签 javascript node.js

我正在尝试创建一个函数,该函数根据搜索条件添加并返回 mysql 表行中的下一个最高值。我的函数如下所示:

    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'))

这给了我:

Promise { pending}

[ RowDataPacket { value: 101 } ]

我想要 [ RowDataPacket { value: 101 } ] 结果,其中显示 promise 待处理。

我也尝试过:

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

这没有给我结果

最佳答案

我看到几个编码错误:

  1. 只有第一个 let newCounter 声明的前面才应该有 let。当您为 newCounter 分配值时,删除 let。那就是创建一个新的本地范围(到本地 block )变量,为其分配一个值,然后它超出范围,并且顶级 newCounter 永远不会获取值。

    <
  2. 赋值是用 = 完成的,而不是用 == 完成的,除非你想要的变量只是一个 bool 值。因此,将 let newCounter == wait pool.query(...) 更改为 newCounter == wait pool.query(...)

    <
  3. 所有async函数都会返回一个promise,并且您必须对调用返回的promise使用.then()await getNextNumber()。因此,一旦修复了其他编码错误,您第二次尝试 console.log(await getNextNumber('QC')) 应该会起作用。

然后,还有一件事需要检查。

您是否 100% 确定您的数据库版本正在从 pool.query() 返回 promise ? await 仅当您正在等待的值是一个 promise 并且它不包含使其他类型的异步操作实际等待并返回值的魔法时,才会做一些有用的事情。 await 需要与 Promise 一起使用。

<小时/>

将它们放在一起(并假设您使用返回 promise 的正确 pool.query() 接口(interface):

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);
});

关于javascript - 异步等待 promise 待处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57317736/

相关文章:

javascript - Google Analytics 服务器端授权获取页面浏览计数分析数据并将其显示在首页上的随机访问者

node.js - Google 网络字体在 Express 中不起作用

node.js - Azure 上的 prerender.io Node.js 预渲染服务器

javascript - 无法从 admin-on-rest (reactJS) 发送正确的 JSON 参数

node.js - MongoDB:按地理位置给定区域和最大点数的集群文档?

javascript - __dirname 未在 Node 14 版本中定义

javascript - Reactjs 滚动多个引用

javascript - 使用 forEach 显示 React Native Maps 中的标记

javascript - 一个字段 javascript 中缺少以美分计的零

javascript - 欢乐 javascript 导入