javascript - (JS, Promise) 如何等待来自多个 map 函数的 Promise
问题描述
我有三个数组:
const firstArray = ['a', 'b', 'c']
const secondArray = ['i', 'j', 'k']
const thirdArray = ['x', 'y', 'z']
我必须使用所有可能的组合发送多个请求,并等待所有响应得到解决,然后再继续。所以我试着这样做
const getPromises = () => {
const promises = firstArray.map(first => {
return secondArray.map(second => {
return thirdArray.map(third => {
return axios.get(`https://someurl.com/${first}/${second}/${third}`)
.then(response => response);
})
})
})
return Promise.all(promises)
}
我有另一种方法,我试图从这个方法中获取返回值
const getvalues = async () => {
const someVariable = await getPromises();
}
但它没有成功。变量someVariable
只是有未解决的承诺。
我做错了什么?
解决方案
我会这样做:
const getPromises = () => Promise.all(function* () {
for (let first of firstArray)
for (let second of secondArray)
for (let third of thirdArray)
yield axios.get(`https://someurl.com/${first}/${second}/${third}`)
}());
推荐阅读
- c# - 在 Unity 中自动为脚本添加自定义编辑器的快捷方式
- angular - 如何使用 Angular 2 中的指令在宿主元素上设置计算宽度
- java - 如何防止 proguard 从输出 jar 中删除“module-info.class”?
- android - 如何使用 FileProvider 在内部存储/缓存中创建和显示 PDF?
- amazon-web-services - 使用 JavaScript 开发工具包 --no-include-email 标志的 AWS ECR 登录
- javascript - toUpperCase 不起作用
- yaml - CircleCi 2.0 工作流程需要选项不起作用
- javascript - JQUERY 中的 $(window).load 未触发
- django - 为通过 imagefield django 上传的图像提供唯一名称
- java - 在 Spring 应用程序中设置时区