Issue in reading subfolder files

Hi all,
I want following:
There is a source folder in which there is one file and one subfolder. The subfolder also has one file. I want to get the both source and subfolder files. I have written following

code:
const fs = require('uxp').storage.localFileSystem; 
    let docs = [];
    let folder = await fs.getFolder();
    const entries = await folder.getEntries();
    var files = await entries.filter(async entry => {
        if (entry.isFile) {
            docs.push(entry);
        }
        else {
            const newEntries = await entry.getEntries();
            await newEntries.filter(async (e) => {
                if (e.isFile) {
                    docs.push(e);
                }
            })
        }
    });

But the issue is that when it enter into else case to read subfolder files, it act asynchronously(the program execution moves ahead) even though I used await keyword. Means I have to wait for 1-2 milliseconds to read subfolder files.
Can anyone please help? Am I missing something?

I see a few issues with your piece of code.

  1. filter() usage - I’m not even sure how this works if you don’t return anything. I believe filter requires that you return a boolean to actually filter items. If you only want to change the docs array, then filter is completely misused here (also filter() isn’t an async function IIRC, so await ...filter() doesn’t really make sense)
  2. 1st point leads to the next thought, that you probably wanted to use a simple forEach(), which leads to the next point…
  3. I suspect filter() works very similarly to a forEach(), which doesn’t handle async code very well (in my experience almost never). You should use a for ... of loop syntax