Return value not fulfilled

22 Views Asked by At

I don't understand why the last console.log returns 'Promise { pending }' without any log. When I can see the result when I uncomment the console.log in the GetInfo function. How can I fix that issue and get my records array from GetInfo?

const fs = require("fs")
const { parse } = require("csv-parse")

const processFile = async () => {
  const records = []
  const parser = fs.createReadStream("bom.csv").pipe(
    parse({
      delimiter: ",",
      columns: ["BoMLevel","Level","Item","Desc","Type","Count"],
    })
  )
  for await (const record of parser) {
    records.push(record)
  }
  return records
}

async function GetInfo() {
  const records = await processFile()
  // console.info(records)
  return records
}

console.log(GetInfo())
1

There are 1 best solutions below

0
Samathingamajig On

You're not awaiting the response of GetInfo.

Either do

GetInfo().then((records) => console.log(records);

or wrap in an async IIFE

(async () => {
  console.log(await GetInfo());
})();

Or just say await GetInfo() if you have top-level await enabled