Lodash的承诺

时间:2018-07-08 12:39:42

标签: javascript lodash

我正在尝试使用_.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' }
]

2 个答案:

答案 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中指出这一点。