executeAsModal doesn't work

Greeting everyone!

After longer period of time, I am back with another question. I am trying to get my program to go through files in a directory and open them one by one in Photoshop. I managed to sort out the files properly to open them in the correct order but now I run into another issue. When I try to open the files, I get an error that the action I want to do must be inside modal scope. However, the function is called using executeAsModal which doens’t make sense. See for yourself (openNextFile is the function in question).

async function runModal(command) {
    try {
        if (command == "start") {
            await require('photoshop').core.executeAsModal(selectFile);
        } else if (command == "next") {
            await require('photoshop').core.executeAsModal(openNextFile(false));
        } else if (command == "previous") {
            await require('photoshop').core.executeAsModal(openNextFile(true));
        } else {
            console.log("None of the commands were fulfilled")
        }
async function openNextFile(reverse) {
    console.log("next file");
    let entry;
    if (reverse) {
        currIndex--;
        entry = workFolder.get(Array.from(workFolder.keys())[currIndex]);
    } else {
        currIndex++;
        entry = workFolder.get(Array.from(workFolder.keys())[currIndex]);
    }
    await openFile(entry);
}
async function openFile(entry) {
    try {
        const app = require("photoshop").app;
        if (entry.isEntry) {
            const document = await app.open(entry);
        } else {
            console.log("the file is not an entry")
        }

    } catch (e) {
        console.log(e);
    }
}

If there is need for any more of my code, just let me know. I use manifest v5 btw. Any help is appreciated.

I think the problem are these two lines:

await require('photoshop').core.executeAsModal(openNextFile(false));
await require('photoshop').core.executeAsModal(openNextFile(true));

You have to pass a function to executeAsModal. In those two lines you’re passing the result of a function call and not a function (there’s a difference) and therefore that function call happens before executeAsModal is called. If you want to call executeAsModal with a function while also passing an argument (i.e. when executeAsModal is calling your function it calls it with that argument) I would suggest passing an arrow expression. Another option could be to just create two functions for either argument case (since that argument you pass can only have two values, true or false), which internally call openNextFile.

Side note: I might take a look at the docs again for executeAsModal. The target function needs to have a certain signature (e.g. passing execution context etc.). If you search on the forum you’ll find a few examples of how people have wrapped that function in ways that make it a bit more straight forward to pass arguments, etc.

see this please