How to read file with async/await properly?

node.jsAsynchronousReadfile

node.js Problem Overview


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.

node.js Solutions


Solution 1 - node.js

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 2 - node.js

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 3 - node.js

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 - node.js

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 - node.js

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 - node.js

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 - node.js

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 - node.js

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

Solution 9 - node.js

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 - node.js

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 - node.js

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 - node.js

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeremy DicaireView Question on Stackoverflow
Solution 1 - node.jsJoelView Answer on Stackoverflow
Solution 2 - node.jstadmanView Answer on Stackoverflow
Solution 3 - node.jsHKTonyLeeView Answer on Stackoverflow
Solution 4 - node.jsarnaudjnnView Answer on Stackoverflow
Solution 5 - node.jsShlomi SchwartzView Answer on Stackoverflow
Solution 6 - node.jsMOSIView Answer on Stackoverflow
Solution 7 - node.jsNonCreature0714View Answer on Stackoverflow
Solution 8 - node.jsGeorge OgdenView Answer on Stackoverflow
Solution 9 - node.jsBesufkad MenjiView Answer on Stackoverflow
Solution 10 - node.jsFakt309View Answer on Stackoverflow
Solution 11 - node.jsArin YazilimView Answer on Stackoverflow
Solution 12 - node.jsMaxProfit GlobalView Answer on Stackoverflow