我是Typescript的新手,这是我第一次接触async
和await
。我有一个express
端点,它不是异步的。内部有一个for循环,似乎需要等待。在TypeScript中遍历此循环的常用方法是什么?
app.get("/", function (req, res) {
const resultIterator = client.query(
'SELECT username FROM users;'
);
for await (const row of resultIterator) { <--- await illegal here
// 'Hello world!'
}
});
答案 0 :(得分:1)
app.get("/", async function (req, res) {
Express端点不在乎它们是否异步。您可以始终使它们异步。
您需要async关键字才能等待工作。