Opening multiple files in sequence

I’m trying to open one file after another in a loop and afterwards have a function processing them, but they seem to be opened all at the same time although I use “await”. What could that be?

Here’s the code …

filearray.forEach(async function(file) {
        try {
          let docRef = app.activeDocument;
          await docRef.flatten();
          await someFunction();
        } catch (error) {
          console.log("File open error.")

It seems to work when I use a “for” loop instead of “forEach”.

forEach loops don’t really work well with async due to the way they’re implemented.
If you need to execute multiple async calls in sequence, you’ll have to use a classic for loop or a for...of loop.

1 Like

Yeah. I’ve realized this now. Also saving has issues with forEach-loops.