我有一个SQL查询,我想先完成操作,然后再继续其他操作。问题是查询调用是异步的。我该如何解决这个问题
const client = new Client({
user: 'postgres',
host: 'localhost',
database: 'postgres',
password: 'password',
port: 5432
})
client.connect()
const playerurls = []
const query = {
name: 'get player url',
text: 'SELECT playerurl FROM players ORDER BY name'
}
client.query(query, (err, res) => {
if(err){
console.log(err)
}
else {
for(let i = 0; i < res.rowCount; i++){
playerurls[i] = res.rows[i].playerurl
}
}
})
console.log(playerurls[20])
现在,如果我想在我的for循环之外访问我的playerurl,它将返回undefined。如何等待查询完成然后继续?