Click here to Skip to main content
15,394,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to write a function that checks if a directory is empty and returns true if it is and false if it is not. But because of the async nature of node.js, the variable I'm trying to give the results of the function is always undefined.

here is the code:

JavaScript
const isDirEmpty = () => {
    let path = "./files_to_move";
    fs.readdir(path, async function(err, files) {
        if (err) {
           throw err;
        } else {
            if (!files.length) {
                console.log("empty");
                return true;
            }
            else {
                console.log("not empty");
                return false;
            }
        }
    });
}

let re = isDirEmpty();
console.log(re);


Thank you in advance.

What I have tried:

I don't really know what to try... if anyone can at least link a resource that I can study from, that would be much appreciated.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900