Javascript 对每个项目执行异步操作,同时执行";“双重迭代”;在数组上

Javascript 对每个项目执行异步操作,同时执行";“双重迭代”;在数组上,javascript,arrays,async-await,Javascript,Arrays,Async Await,我不知道术语是否正确,但我有一个对象数组,其中还包含其他数组。我需要逐一检查这些项目。如果操作不是异步的,则会出现如下情况: myArray.forEach(x => { x.otherArray.forEach(y => { doSomething(y) }) }) let objArray = [ {otherArray: [1,2]}, {otherArray: [3,4]}, {otherArray: [5,6]} ]; let index = 0; let

我不知道术语是否正确,但我有一个对象数组,其中还包含其他数组。我需要逐一检查这些项目。如果操作不是异步的,则会出现如下情况:

myArray.forEach(x => {
  x.otherArray.forEach(y => {
    doSomething(y)
  })
})
let objArray = [ {otherArray: [1,2]}, {otherArray: [3,4]}, {otherArray: [5,6]} ];
let index = 0;
let subIndex = 0;

function doAsyncSomething(item) {
    return new Promise(resolve => {
        console.log("proc item", item);
        resolve(item);
    });
}

async function doit() {
    return await doAsyncSomething(objArray[index].otherArray[subIndex]);
}

function go() {
    doit().then(v => {
        console.log(v);

        subIndex++;
        if (subIndex >= objArray[index].otherArray.length) {
            subIndex = 0;
            index++;
        }
        if (index < objArray.length)
            go();
    });
}
然而,
doSomething
函数是
async
,不幸的是,我清楚地知道,在这些迭代过程中,我不能简单地通过几个异步并等待它工作

通常,当我需要在迭代过程中做出承诺时,我会执行以下操作:

await myArray.reduce((p, item) => {
  return p.then(() => {
    return doAsyncSomething(item)
  })
}, Promise.resolve())
但是因为我同时做两次迭代,这就变得有点复杂了,所以我该怎么做呢

我目前有这样的想法,但这似乎不是正确的方式:

await myArray.reduce((p, item) => {
    return item.someArray.reduce((promise, it, index) => {
      return promise.then(() => {
        return doAsyncSomething()
      })
    }, Promise.resolve())
  }, Promise.resolve())

我知道我可以通过两个
forEach
将我的对象组织成一个数组,然后使用
doSomething
进行
reduce
,但我怀疑这是最有效、最优雅的方法。那么我该如何做到这一点呢?

在减少以下内容时,将承诺传递到内部循环:

  await myArray.reduce((p, item) =>
    item.someArray.reduce((p, it, index) => 
      p.then(() => doAsyncSomething(it)),
      p // <<<
    ), 
    Promise.resolve()
  )

如果要并行运行任务,请执行以下操作:

  await Promise.all(
    myArray.flatMap(item => item.someArray.map(doSomethingAsnyc))
 );

假设希望所有操作并行进行,可以使用
Promise.all()

功能剂量仪(x){
返回新承诺((确定,失败)=>
设置超时(()=>{
控制台日志(x);
ok();
},10));
}
设foo=[[1,2,3,4],[5,6,7,8];
异步函数测试(){
设asyncOps=[];
foo.forEach(x=>
x、 forEach(y=>
asyncOps.push(doSomething(y));
等待承诺。全部(异步操作);
}
test()请尝试以下操作:

let objArray = [ {otherArray: [1,2]}, {otherArray: [3,4]}, {otherArray: [5,6]} ];

function doAsyncSomething(item) {
    return Promise.resolve(item);
}

async function doit() {
    let s = 0;
    for(const x of objArray)
        for(const y of x.otherArray)
            s+= await doAsyncSomething(y);

    return s;
}

doit().then(v => {
  console.log(v);
});
或者尝试这样的反循环呼叫:

myArray.forEach(x => {
  x.otherArray.forEach(y => {
    doSomething(y)
  })
})
let objArray = [ {otherArray: [1,2]}, {otherArray: [3,4]}, {otherArray: [5,6]} ];
let index = 0;
let subIndex = 0;

function doAsyncSomething(item) {
    return new Promise(resolve => {
        console.log("proc item", item);
        resolve(item);
    });
}

async function doit() {
    return await doAsyncSomething(objArray[index].otherArray[subIndex]);
}

function go() {
    doit().then(v => {
        console.log(v);

        subIndex++;
        if (subIndex >= objArray[index].otherArray.length) {
            subIndex = 0;
            index++;
        }
        if (index < objArray.length)
            go();
    });
}
让objArray=[{otherArray:[1,2]},{otherArray:[3,4]},{otherArray:[5,6]}];
设指数=0;
设子索引=0;
功能(项目){
返回新承诺(解决=>{
console.log(“proc项”,item);
解决(项目);
});
}
异步函数doit(){
return wait wait doAsyncSomething(objArray[index].otherArray[subIndex]);
}
函数go(){
doit()。然后(v=>{
控制台日志(v);
子索引++;
if(子索引>=objArray[index].otherArray.length){
子指数=0;
索引++;
}
if(索引
Nope,
wait
只能出现在
async
函数中。是。假定代码段位于异步函数内。请参阅原始的postThanks,感谢您的报复投票:)不过您仍然应该尝试该代码。。。(你知道
y=>{
是一个常规的非异步函数吗?@jonaswillms我没有投票。正如你所看到的,我的代码是有效的。查看代码段你知道我没有推
y
。我推了
doSomething()
这是一个异步函数的结果