异步Json阵列矩阵



(如果这是一个糟糕的解释,请道歉(

我想知道是否有更好的理由这样做:

const arr1= ['cat', 'dog']
const arr2 = ['brown', 'gray']
return await Promise.all(arr1.map(async (one) => {
return await Promise.all(arr2.map(async (two) => {
return await AnotherAsyncFunction(one,two)
}))
})).then(res => console.log(res.flat()))

本质上,我有嵌套的promise数组,每个数组都有自己的映射,

其返回来自"0"的响应;AnotherAsyncFunction";在promise.all数组中,

IE:

[{
catbrown,
catgray,
dogbrown,
doggray
}]

这就是我想要的行为,但我想知道是否有一个优雅的解决方案?

或者即使已经有一些东西可以实现相同的结果

提前谢谢。

您不需要return awaits,也不需要嵌套的async函数,只需单独返回Promises即可。

const arr1= ['cat', 'dog']
const arr2 = ['brown', 'gray']
return Promise.all(
arr1.map(one => Promise.all(
arr2.map(two => AnotherAsyncFunction(one,two))
))
)
.then(res => console.log(res.flat()))

最新更新