'How to read file with async/await properly?

I cannot figure out how async/await works. I slightly understand it but I can't make it work.

function loadMonoCounter() {
    fs.readFileSync("monolitic.txt", "binary", async function(err, data) {
       return await new Buffer( data);
  });
}

module.exports.read = function() {
  console.log(loadMonoCounter());
};

I know, I could use readFileSync, but if I do, I know I'll never understand async/await and I'll just bury the issue.

Goal: Call loadMonoCounter() and return the content of a file.

That file is incremented every time incrementMonoCounter() is called (every page load). The file contains the dump of a buffer in binary and is stored on a SSD.

No matter what I do, I get an error or undefined in the console.



Solution 1:[1]

To use await/async you need methods that return promises. The core API functions don't do that without wrappers like promisify:

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);
})

As a note, readFileSync does not take a callback, it returns the data or throws an exception. You're not getting the value you want because that function you supply is ignored and you're not capturing the actual return value.

Solution 2:[2]

Since Node v11.0.0 fs promises are available natively without promisify:

const fs = require('fs').promises;
async function loadMonoCounter() {
    const data = await fs.readFile("monolitic.txt", "binary");
    return Buffer.from(data);
}

Solution 3:[3]

This is TypeScript version of @Joel's answer. It is usable after Node 11.0:

import { promises as fs } from 'fs';

async function loadMonoCounter() {
    const data = await fs.readFile('monolitic.txt', 'binary');
    return Buffer.from(data);
}

Solution 4:[4]

You can use fs.promises available natively since Node v11.0.0

import fs from 'fs';

const readFile = async filePath => {
  try {
    const data = await fs.promises.readFile(filePath, 'utf8')
    return data
  }
  catch(err) {
    console.log(err)
  }
}

Solution 5:[5]

You can easily wrap the readFile command with a promise like so:

async function readFile(path) {
    return new Promise((resolve, reject) => {
      fs.readFile(path, 'utf8', function (err, data) {
        if (err) {
          reject(err);
        }
        resolve(data);
      });
    });
  }

then use:

await readFile("path/to/file");

Solution 6:[6]

From node v14.0.0

const {readFile} = require('fs/promises');

const myFunction = async()=>{
    const result = await readFile('monolitic.txt','binary')
    console.log(result)
}

myFunction()

Solution 7:[7]

To keep it succint and retain all functionality of fs:

const fs = require('fs');
const fsPromises = fs.promises;

async function loadMonoCounter() {
    const data = await fsPromises.readFile('monolitic.txt', 'binary');
    return new Buffer(data);
}

Importing fs and fs.promises separately will give access to the entire fs API while also keeping it more readable... So that something like the next example is easily accomplished.

// the 'next example'
fsPromises.access('monolitic.txt', fs.constants.R_OK | fs.constants.W_OK)
    .then(() => console.log('can access'))
    .catch(() => console.error('cannot access'));

Solution 8:[8]

There is a fs.readFileSync( path, options ) method, which is synchronous.

Solution 9:[9]

const fs = require("fs");
const util = require("util");
const readFile = util.promisify(fs.readFile);
const getContent = async () => {
let my_content;
try {
  const { toJSON } = await readFile("credentials.json");
  my_content = toJSON();
  console.log(my_content);
} catch (e) {
  console.log("Error loading client secret file:", e);
 }
};

Solution 10:[10]

I read file by using the Promise. For me its properly:

const fs = require('fs')

//function which return Promise
const read = (path, type) => new Promise((resolve, reject) => {
  fs.readFile(path, type, (err, file) => {
    if (err) reject(err)
    resolve(file)
  })
})

//example how call this function
read('file.txt', 'utf8')
    .then((file) => console.log('your file is '+file))
    .catch((err) => console.log('error reading file '+err))

//another example how call function inside async
async function func() {
  let file = await read('file.txt', 'utf8')
  console.log('your file is '+file)
}

Solution 11:[11]

You can find my approach below: First, I required fs as fsBase, then I put the "promises" inside fs variable.

const fsBase = require('fs');
const fs = fsBase.promises

const fn = async () => {
    const data = await fs.readFile('example.txt', 'utf8');
    console.log(data);
};

fn();

Solution 12:[12]

see this example https://www.geeksforgeeks.org/node-js-fs-readfile-method/

// Include fs module
var fs = require('fs');
  
// Use fs.readFile() method to read the file
fs.readFile('demo.txt', (err, data) => {
  console.log(data);
})