【发布时间】:2020-10-12 15:57:16
【问题描述】:
app.get("/", function (req, res) {
async function showData() {
const res = await fetch("api.openweathermap.org/data/2.5/weather?q=Berlin&appid={apiID}");
const data = await res.json();
console.log(data);
}
})
//这里当我运行 node app.js 这应该记录终端内的数据,但我得到了错误。
【问题讨论】:
-
为什么在 res.json() 之前使用 await?你能解释一下吗?
-
这里 await before res.json() 类似于 .then。基本上,我在等待 API url 链接之后在 res.json() 之前使用 await 以获取 JSON 格式的响应。
标签: node.js asynchronous async-await