I’m fairly new to programming and I’m diving into some node.js and puppeteer things.
I had a question regarding the difference of use and meaning of rmDir, rmDirSync, rmSync.
I was told to try to use rmSync rather than rmDir, but I am not too sure what the difference and why one would choose one over the other.
I tried to search online but maybe my understanding isn’t too great yet, wanted to see if I could get an explanation that I could grasp.
Initially I am using this to just clear some cacheData.
I have been using rmDir, without the Sync
I wanted to know if I should include the Sync?
My function is within a try catch, if thats important to state…
Sorry if the answer is already out there,
I’ve tried to read and understand but still don’t…
Stuck in a rut in terms of learning and understanding…
3
Answers
The File System (or fs) module of node offers synchronous and asynchronous version of a lot of it’s functions:
The
rmDir
function asynchronously deletes a directory and throws an exception if it’s passed a path to a file to delete.The
rmDirSync
function synchronously deletes a directory and throws an exception if it’s passed a path to a file to delete.The
rmSync
function is the synchronous version of its asynchronous counterpart which isrm
. ThermSync
andrm
functions can delete directories or files (and is modeled on the standard POSIXrm
utility).Depending on whether the design of what you’re working needs asynchronous function or synchronous should dictate which one to use.
->HERE’S<- a link to Node’s File System api that has information on all the functions you were asking about.
If you need more info about asynchronous and synchronous functions, unless you have a specific question about it, there’s tons of documentation on it all over the internet.
fs.rmdirSync
is what in most other languages you would expect fromrmdir
: it removes the directory. The next line in your code can assume that the directory is already there: your program will grind to a halt and wait until the OS does its job. As you might imagine, this might be inconvenient if your process is a web server serving hundreds of users — everyone has to wait till the directory situation is dealt with. But synchronous functions are easy to reason about, since they look like most other languages.fs.rmdir
is asynchronous: it remembers that it should remove the directory when it is free. The directory will be removed some time in the future. If you wish to do something after the directory is sure to be gone, you will have to specify this as a callback. This makes the code a bit harder to read, especially for people who are not used to this model of execution. What is certain is that the callback will not have executed before the next line of code; in fact, not before all the functions that are currently running have exited. However, this leaves the JavaScript execution environment to continue running code while waiting on the OS to delete your directory. This makes the asynchronous functions very useful for code which does many input/output operations (where the JavaScript engine has to wait for their completion).There are a couple of other related things:
fsPromise.rmdir
will wrap the asynchronous function into a promise, which reduces the clutter associated with nested callbacks; and they can be used withasync
/await
keywords, which can make asynchronous code look as if it was synchronous.As you can see from the official rmdirSync docs, it is used to perform the remove directory in a
synchronous
manner(next code execution will stop unless the task is complete) whereas rmdir is an asynchronous process which will delete the folder then thecallback
will be implemented where you can get whether the task waserror
orsuccess
(meanwhile the next code execution continues).