0

I have a few async functions that need to work in nested for loops but the await fails if it's not in the first for loop, or in the base level function.

I tried moving my await up and down the levels of the for loop, and found that after the first for loop, it fails.

Any suggestions what I should try?

async function Func2() {
    //DO ASYNC STUFF
};

async function Func3() {
    //DO ASYNC STUFF
};

async function Func1() {
    var Data = await Func2();             //THIS AWAIT WORKS
    for (var X1 = 0; X1 <= 10; X1++) {
                                          //AWAIT WOULD WORK HERE
        Object.keys(Data[0]).forEach(function (key2) {
                                          //BUT NOT HERE.
            Object.keys(rows).forEach(function (key) {
                var Data = await Func3(); //THIS AWAIT ERRORS: "await is only valid in async function"
            });
        })
    };
}

Func1();
G.H.
  • 199
  • 10
  • Using for loops is way easier than using forEach! – Evert Jan 24 '21 at 06:02
  • I realized (after a few hours) that there were 2 problems - my `forEach(function()` should be `forEach(async function()` and secondly, async/await doesn't work inside a `forEach` - instead you need to use a variable `for` loop. – G.H. Jan 24 '21 at 22:19
  • Yeah correct =) – Evert Jan 24 '21 at 23:28

0 Answers0