I used 3 different File System APIs in Node.js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the most common things that you'll do on a server-side application is read and write to the file system confusingly node.js provides three different ways to get this job done let's look at all three of them and compare the trade-offs here's what i want to do i want to read a directory and then remove all of the files in that directory without deleting the directory itself the original flavor api is callback based use read directory to get all the files in a directory and remove to delete a file the benefit of the callback api is that the actual file operations happen asynchronously off of the main thread that means if you have additional code to run after the file operations it will not be blocked and runs immediately while all the file stuff happens in the background the thing that sucks about callbacks is that they're very awkward to work with after reading a directory we can access the files in a callback function this function has two arguments the first one is an error object that will only be defined if it fails and the second one is the actual files that are in that directory now if you get an error you'll want to manually throw an error in your code from there we can loop over the files and remove them which also takes a callback function to handle errors as you can see here we have nested callbacks which is also known as callback hell code like this can get really ugly to deal with but luckily node has a different api that can simplify things for every file system function there's a corresponding sync function that uses the synchronous api we can use this api without callbacks but the drawback is that it blocks the main thread so your code will wait for that file system operation to be complete before it moves on to the next line of code if that's not a problem you can get everything done on a single line of code as i'm doing here but what if there was a third way that could give us the best of both worlds a relatively new node api is fs promises it has all the same functions found in the callback api but instead they're promise based so we can first read the directory then use map to loop over the files and convert each one into a removal operation promise now the tricky thing here is that with async await you'll want to execute all the removals in parallel and the mechanism to do that is promise.all it's not as simple as the synchronous api but this allows us to do things asynchronously with far better ergonomics than the callback api and finally if none of these apis are good enough for you you might also look into libraries like fs extra that provide a bunch of utilities to extend node.js if you're willing to add extra dependencies to your project that's how you work with the file system in node.js hit subscribe if you want to learn more thanks for watching and i will see you in the next one
Info
Channel: Beyond Fireship
Views: 109,472
Rating: undefined out of 5
Keywords:
Id: msnTtrx2C4s
Channel Id: undefined
Length: 2min 19sec (139 seconds)
Published: Mon Sep 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.