fs read in async code example

Example 1: how to export fs.readFile

var fs = require('fs');

module.exports = {
    parseFile: function(file_path, callback) {
        fs.readFile(file_path.toString(), 'utf-8', function(err, data) {
            if (err) return callback(err);
            callback(null, data);
        });
    }
}

// much shorter version
exports.parseFile = function(file_path, callback) {
    fs.readFile(file_path.toString(), 'utf-8', callback);
}

Example 2: readfile async nodejs

const fs = require('fs');
const util = require('util');

// Convert fs.readFile into Promise version of same    
const readFile = util.promisify(fs.readFile);

function getStuff() {
  return readFile('test');
}

// Can't use `await` outside of an async function so you need to chain
// with then()
getStuff().then(data => {
  console.log(data);
})