First Blog!

Hi whoever come here,

I have been coding for quite long, and this is the first time I created a blog for my studies. Here are some of the results of my researches and learning hours on the subject of Node js fs module.

1. fs.unlink(path, callback)

This function is used to deletes a file or symbolic link Asynchronously. When a callback is done, no argument except a possible exception will be given. It will not block your code, and will call a callback function once the file has been removed.

// Assuming that 'path/file.txt' is a regular file.
fs.unlink('path/file.txt', (err) => {
if (err) throw err;
console.log('path/file.txt was deleted');
});

2. fs.unlinkSync(path)

Not like unlink, fs.unlinkSync(path) delete file synchronous manner.It is synchronously, so the synchronous call will cause your code to block and wait until the file has been removed.

const fs = require('fs')

const path = './file.txt'

try {
  fs.unlinkSync(path)
} catch(err) {
  console.error(err)
}

+ References:
https://flaviocopes.com/how-to-remove-file-node/  http://www.technicalkeeda.com/nodejs-tutorials/how-to-delete-file-using-nodejs

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s