rmSync function from node.js fs module not throwing EBUSY exception

Viewed 631

I'm trying to delete a folder recursively in synchronised manner using rmSync function of fs module. I've enclosed this function invocation inside of try/catch block to handle error when any file from the folder to be deleted is being used by some other process. But even when I try opening a file and write and save contents in them, it doesn't raise an EBUSY exception for it. However when I tried the same thing with its asynchronous counterpart (i.e rm) I'm able to get that error in the callback function.

Here's the code for both the scenarios:

Synchronous

const removeFolder = (folderPath) => {
    try {
        fs.rmSync(folderPath, {recursive: true})
    } catch(error) {
        console.log(error.message);
    }
}

Result: The code doesn't raise an EBUSY exception

Asynchronous

const removeFolder = (folderPath) => {
    fs.rm(folderPath, {recursive: true}, (error) => {
        if (error) console.log(error.message);
    });
}

Result: EBUSY: resource busy or locked, rmdir

I'm using the latest version of node v14.17.0. What am I missing here?

0 Answers
Related