How Would You Loop Through Open Documents

I have found a situation where I would like to loop through all open documents and do something on each document but I am not sure how to acomplish it.

Having read some online docs on loops, I see there are different types which has confused me a little.

Any help would be appreciated

As you are likely going to need to use asynchronous code for whatever you’re going to do to each document you should avoid using foreach loops as they do not allow asynchronous code.

I’d use:

for (const document of app.documents) {
    // Do stuff to each document - the document variable will update to reflect iterating over each document on each loop

because it’s easier to read and allows for asynchronous code.

Thank You, I can probably work with that. I never knew that foreach dont allow async so thats also helpful.