Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions reference/fs_promise_demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fs = require('fs').promises;
const path = require('path');

// Create folder, this is using asynchronous Promise file system API
fs.mkdir(path.join(__dirname, 'test'))
.then(() => console.log('Folder created...'))
.catch(error => {
if (error.code === 'EEXIST') {
console.log('Folder already exists...');
} else { throw error; }
});

const filePath = path.join(__dirname, 'test', 'hello.txt');

// Create and write to file
fs.writeFile(filePath, 'Hello World!')
.then(() => console.log('File written...'))
.then(() =>

// File append
fs.appendFile(filePath, '\nI love NodeJS')
).then(() => {

// Read file
fs.readFile(filePath, 'utf-8')
.then(data => console.log(`\n"${data}"\n`));
}).then(() =>

// Rename file
fs.rename(
filePath,
path.join(path.dirname(filePath),
'helloworld.txt')
).then(() => console.log('File renamed...'))
);