Read file with promise in Node.js

Reading Time: < 1 minutes

Promises are a great way to improve your codebase by preventing callback hell and making asynchronous code look more readable. Working with files in Node.js can be done with callbacks if you’d like, but promises can make it easier. In this post, we’ll go over how to read and write files with promises in Node.js.

      
const {promises: {readFile}} = require("fs");

const content = await readFile('./test.txt', {encoding:'utf8', flag:'r'});

      
    
,

About the author

Andrés Canavesi
Andrés Canavesi

Software Engineer with 15+ experience in software development, specialized in Salesforce, Java and Node.js.


Related posts


Leave a Reply

%d bloggers like this: