1

i have a function i am trying to read a file from the same directory it is showing promise pending what needs to be fixed here

index.js

const fs = require('fs').promises;
const path = require('path');
const filePath = path.join(__dirname, 'Accounts.txt');
async function ReadFile() {   
  try {
    let data = await fs.readFile(path.join(filePath));
    data = String(data)
    return data;
 } catch(e) {
   console.log(e);
 } 

Console

READING FILE>>>> Promise { <pending> }
[0] (node:6379) ExperimentalWarning: The fs.promises API is experimental
4
  • 1
    you need to use fs.readFileSync(...) Commented Dec 20, 2021 at 20:41
  • readFileSync isn't exported from fs promises. Commented Dec 20, 2021 at 20:43
  • You need to resolve it either by using another await inside An IIFE (Immediately Invoked Function Expression) like: (async function () { console.log(await ReadFile()); })(); Commented Dec 20, 2021 at 20:45
  • using then() like that : ReadFile().then((x) => console.log(x)); Commented Dec 20, 2021 at 20:48

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.