我正在尝试使用_.times生成用户列表,但得到了一组Promised对象。如何输出普通对象数组?
const createUser = async () => ({
});
const users = [];
_.times(5, () => users.push(createUser()));
Promise.all(users);
输出
[ Promise { { username: 'Armando', password: '8cw0W7Jpm0LSSS9' } },
Promise { { username: 'Lauren', password: '9oFCK4rqLj_DGol' } },
Promise { { username: 'Frederique', password: 'JXGOsjCCOMFBYFd' } },
Promise { { username: 'Otilia', password: 'DnxArNIsjaVoMB2' } },
Promise { { username: 'Elisabeth', password: 'kZbSlg7bVWiagFT' } } ]
预期
[
{ username: 'Armando', password: '8cw0W7Jpm0LSSS9' },
{ username: 'Lauren', password: '9oFCK4rqLj_DGol' },
{ username: 'Frederique', password: 'JXGOsjCCOMFBYFd' },
{ username: 'Otilia', password: 'DnxArNIsjaVoMB2' },
{ username: 'Elisabeth', password: 'kZbSlg7bVWiagFT' }
]
答案 0 :(得分:4)
您可以在<div class="main">
<div class="results">
<div class="name">joan</div>
<div class="age">32</div>
</div>
<div class="results">
<div class="name">Sam</div>
<div class="age">35</div>
</div>
<div class="results">
<div class="name">joo</div>
<div class="age">32</div>
</div>
</div>
的{{1}}回调中获得它:
then
答案 1 :(得分:4)
await
的结果Promise.all
:
const users = await Promise.all(userPromises)
编辑
使用await
当然仅在async
函数内部起作用-thx可以在注释Olian02中指出这一点。