Answers for "read file async nodejs"

7

nodejs readfile

const fs = require('fs');

fs.readFile('/Users/joe/test.txt', 'utf8' , (err, data) => {
  if (err) {
    console.error(err);
    return
  }
  console.log(data);
});
Posted by: Guest on August-30-2020
0

async file read node js

function firstFunction(_callback) {
    // do some asynchronous work
    // and when the asynchronous stuff is complete
    const fs = require('fs');
    let jsonData;
    fs.readFile("industriesByCat.txt", 'utf8', function (err, data) {
        if (err) throw err;
        jsonData = JSON.parse(data);
        console.log(jsonData);
      	
      	//This is important for callback
        _callback();
    });

}

function secondFunction() {
    // call first function and pass in a callback function which
    // first function runs when it has completed
    firstFunction(function () {
        console.log('huzzah, I\'m done!');
    });
}
secondFunction();
Posted by: Guest on September-10-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language