我需要得到一个特定的所有用户,并有一个具有他们的特定数据的API (例如:https://example.com/api/userType/1/),该数据存储了具有其唯一API的所有用户的数组,例如
{
name: "Type 1",
...
users: [
"https://example.com/api/user/1/",
"https://example.com/api/user/18/",
"https://example.com/api/user/40/",
...
]
...
}我想向所有这些用户展示普通的JS,特别是fetchData函数。我认为进行一次API调用,然后遍历该API调用中的所有用户(在then promise函数中) --对他们唯一的API url进行另一次API调用是不明智的。我在考虑用两个函数创建一个闭包函数来进行API调用,但是我不确定这样做的最佳实践是什么。对如何正确执行此操作有什么建议吗?
发布于 2018-04-03 01:31:48
async function getUsers(){
const { users } = (await fetch ("https://example.com/api/userType/1/")).json();
return Promise.all(users.map(user => fetch(user).then(req => req.json()));
}https://stackoverflow.com/questions/49615774
复制相似问题