【发布时间】:2021-05-10 06:16:35
【问题描述】:
我是新来的反应。我有一个在组件安装时获取一次的用户列表。 在将结果发送到reducer之前,它需要在用户列表中循环并从另一个端点获取用户全名/标题,然后向用户列表对象添加一个新属性。 我无法弄清楚如何在调用调度之前等待所有承诺(getUserById() 函数)完成。我在这里尝试了解决方案,但失败了: How to return many Promises and wait for them all before doing other stuff
下面的代码只是为了说明我想要什么:
const {
listUsers,
fetchUserData
} = useContext(GlobalContext);
const getUserById = async (userId) => {
return sp.web.siteUsers.getById(userId).get().then(user => user.Title);
}
useEffect(() => {
sp.web.lists.getById("8C271450-D3F9-489C-B4FC-9C7470594466").items.get()
.then(userLists => {
userLists = userLists.map(list => {
if (list.Person_x0020_ResponsibleId) {
getUserById(list.Person_x0020_ResponsibleId).then(username => {
list['Person_Responsible'] = username; // -> fetch user fullname and title
})
} else { // -> if id is null
list['Person_Responsible'] = '-';
}
return list
});
fetchListSuccess(userLists); // -> dispatch result to reducer
});
}, []);
【问题讨论】:
标签: javascript reactjs react-context