可以在節點V7 +使用async
/await
做Promise
鏈:
router.get('/', async (req, res, next) => {
await sql.connect(config)
try {
const projects = await sql.query(`select Project_Type_Desc from Project_Type`)
const result2 = await sql.query(`another query`)
const result2 = await sql.query(`another query`)
const result4 = await sql.query(`another query`)
res.render('newProject', {
projects,
result2,
result3,
result4
})
} catch (error) {
console.log(error)
}
})
同時運行的承諾,用Promise.all
:
router.get('/', async (req, res, next) => {
await sql.connect(config)
const promises = Promise.all([
await sql.query(`select Project_Type_Desc from Project_Type`),
const result2 = await sql.query(`another query`),
const result2 = await sql.query(`another query`),
const result4 = await sql.query(`another query`)
])
try {
const [projects, result2, result3, result4] = await promises
res.render('newProject', {
projects,
result2,
result3,
result4
})
} catch (error) {
console.log(error)
}
})
我試過第一種方法,並得到以下錯誤:https://pastebin.com/Uwn4A8rC你可能是什麼?即時通訊使用mssql節點模塊連接到mssql服務器 – SSS