【发布时间】:2020-10-03 03:11:05
【问题描述】:
我使用 Promise all 同时获取 2 个 URL,但是当我使用 await 调用此函数时(因为 getAllURLs 是异步函数),它给了我一个错误, 我怎么解决这个问题?
const fetch = require("node-fetch");
let urls = ["https://jsonplaceholder.typicode.com/users","https://jsonplaceholder.typicode.com/users"]
async function getAllUrls(urls) {
try {
var data = await Promise.all(
urls.map((url) => fetch(url).then((response) => response.json()))
);
return data;
} catch (error) {
console.log(error);
throw error;
}
}
const response = await getAllUrls(urls)
console.log(response)
错误:
let responses = await getAllUrls(urls)
await is only valid in async function
【问题讨论】:
标签: javascript node.js express fetch