【发布时间】:2019-12-16 18:26:37
【问题描述】:
我尝试使用 nodeJS 以顺序方式调用 2 个对 postgres 的查询。我来到了这个代码。 但是我对此并不满意,做一件简单的事情似乎过于复杂。有没有更简单的方法来做我想做的事情?
基本上,我尝试在控制台上打印“before 1st”,然后执行 select now(),然后打印结果,然后写入“after 1st”,然后打印“before 2nd”,然后执行 select now(),然后打印结果,然后写“第二次之后”
getConn().
then(async() => {
console.log("before 1st selectNow2")
await selectNow2()
console.log("after 1st selectNow2")
})
.then(async() => {
console.log("before 2nd selectNow2")
await selectNow2()
console.log("after 2nd selectNow2")
})
async function selectNow2() {
await client.query('SELECT NOW()')
.then(res => {
console.log(res.rows[0])
})
.catch(err => {
console.log(err.stack)
})
}
【问题讨论】:
标签: node.js promise async-await sequential