Message from JavaScript discussions
September 2017
— I mean, I'm no expert, but that sounds like a bite too big for JS lol
Hello everyone, please i just started learning js so i am going through a video by net ninja on node.js... but when i run this code to create a directory and also create a file write.txt in it containing the content from the read.txt file it is just not showing anything in the new directory i created.. What could have gone wrong???
var fs = require('fs');
fs.mkdirSync('stuff', function(){
fs.readFile('read.txt', 'utf8', function(err, data){
fs.writeFile('./stuff/write.txt', data);
});
});
— Remove "Sync"?
— It worked thanks, totally did not see that
— Also look at util.promisify
—
const fs =
require('fs');
const { promisify } =
require('util');
const mkdir =
promisify(fs.mkdir);
const readFile =
promisify(fs.readFile);
const writeFile =
promisify(fs.writeFile);
mkdir('stuff')
.then(() =>
readFile('read.txt'))
.then(data =>
writeFile('stuff/write.txt', data));
— Okay const means constant and that variable fs can't be changed or reassigned
— Yep
— Always use const unless you plan to reassign
— Okay thank u i will note that down now
— Not really, all it is, is an event loop and cooperative scheduler, things that can be used for any purpose
— It basically lets seperate things get processing time without returning