【发布时间】:2018-07-12 13:14:39
【问题描述】:
为什么这段代码 sn -p 没有按预期工作,以至于在对象填充数据之前调用了console.debug(images)?我希望这两个循环并行运行,但await Promise.all 应该等待循环完成。第一个循环和第二个循环应该同时运行。
const images = {
taskImages: [],
solutionImages: []
};
await Promise.all(Object.keys(files).map((key) => {
files[key].map(async (file) => {
const fileId = getFileId(file.path);
const result = await storeImage(fileId, file.path);
if (result) {
images[key].push(fileId);
console.debug("Pushed " + key);
}
});
}));
console.debug(images);
【问题讨论】:
-
您必须在变量上定义等待。
const promises = await Promise.all([]); -
@Baruch 我不这么认为。
标签: javascript node.js loops promise