node-csvtojson icon indicating copy to clipboard operation
node-csvtojson copied to clipboard

Thennable/Promise Implentation

Open evanshortiss opened this issue 5 years ago • 1 comments

Hey, opening this per suggestion in #339

The returned fromFile() Promise is not spec compliant so it does not resolve if you call then or await on it at a later point.

For example, this code hangs forever on the await, but with a standard Promise implementation it would work:

const dataPromise = csv().fromFile('my-data.csv'))

async function doSomeStuff (name) {
  const data = await dataPromise // This will hang forever!

  return data.find(d => d.name === name)
}

evanshortiss avatar Sep 11 '19 11:09 evanshortiss

Until a solution is PR'd you can simply do this if you'd like to start parsing but don't want to immediately wait for the result:

const dataPromise = csv().fromFile('my-data.csv')).then((result) => Promise.resolve(result))

async function doSomeStuff (name) {
  const data = await dataPromise // Yay, it works now!

  return data.find(d => d.name === name)
}

evanshortiss avatar Sep 11 '19 11:09 evanshortiss