0

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())
M.K.
  • 3
  • 2

1 Answers1

0

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

Samathingamajig
  • 11,839
  • 3
  • 12
  • 34