javascript - 为什么我的嵌套等待函数没有等待
问题描述
我不明白为什么下面的函数运行得这么快,底层异步函数在 0.06 秒内运行,而不是我预期的 2.06 秒(在 sleep 函数之后)。
const hiWorld = () => {
const final = await Promise.all(
firstLevelArray.map(async (secondLevelArray) => {
const topLevelResponse = await Promise.all(
secondLevelArray.map(async (item) => {
const asyncResponse = await myAsyncFunction({
item: item,
});
console.log(new Date()) // this shows that it's running within .06 seconds instead of 2.06 seconds
await new Promise(r => setTimeout(r, 2000));
return asyncResponse
})
);
return topLevelResponse
})
);
return final
}
如果它很重要,这与 Typescript 一起使用。
这是console.log:
2021-04-07T01:45:02.501Z
2021-04-07T01:45:02.552Z
2021-04-07T01:45:02.619Z
解决方案
推荐阅读
- reinforced-typings - Reinforced.Typings 正在 MacOS 上寻找 dotnet.exe,因此无法运行
- debugging - 如何生成仅在失败时才详细的 make 构建
- javascript - 从将所有文件转换为 es6 的项目中删除 Typescript
- python - Panda Pivot 功能无法正常工作
- razor - SfGrid RowSelection 事件处理重新加载数据
- python - 如何从 Django“Choices”中获取值并通过下拉元素将它们作为 Axios 请求发送到 React 前端?
- python - 使用 keras 预处理后绘制图像
- graphql - GraphQL:在所有解析器之间共享上下文
- linux - 安装pdftk alpine linux时出现不可满足的约束错误
- python - 为什么 DataFrame.drop() 不适用于列方式(轴 = 1)?