我有以下代码,fileStatsPromises
is of Promise<Stats>[]
, bothfoo
和bar
are Promise<Stats>[]
。等待他们的正确方法是什么?我想得到<Stats>[]
。
const files = await readDir(currentDir);
const fileStatsPromises = files.map(filename => path.join(currentDir, filename)).map(stat);
const foo = await fileStatsPromises;
const bar = await Promise.all(fileStatsPromises);
编辑:一个最小的例子。
function makePromise() {
return Promise.resolve("hello");
}
const promiseArray = [];
// const promiseArray = [] as Promise<string>[];
for (let i = 0; i < 10; i++) {
promiseArray.push(makePromise());
}
(async () => {
const foo = await promiseArray;
const bar = await Promise.all(promiseArray);
})();