question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Await error when running documentation example

See original GitHub issue

I’m trying to run the following example, shown in documentation:

const jsonArray=await `csv().fromString(myCSV);

I tried it like this:

const myCSV='col0,col1,col2\n1,2,3\n4,5,6';
const jsonArray=await csv().fromString(myCSV);
console.log(jsonArray);

But I’m getting:

SyntaxError: await is only valid in async function

Is this expected in the current version? I tried the solution given in issue https://github.com/Keyang/node-csvtojson/issues/272, but got the same problem.

I’m using version csvtojson@2.0.8, node v8.12.0.

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Keyangcommented, Oct 10, 2018

As #272 pointed out, await can only be used in an async function. try this:

async function run(){
const myCSV='col0,col1,col2\n1,2,3\n4,5,6';
const jsonArray=await csv().fromString(myCSV);
console.log(jsonArray);
}
run();
0reactions
WORMSScommented, Apr 11, 2019

@punkish your ‘run’ function is asynchronous, you would need to await the results of it, which is exactly the same as awaiting the results of fromFile. You would basically end up with

(async function () {
  return run();
})();

But you could not output that to module.exports. Basically, you cannot output anything from this module synchronously.

Even if you were to fill the csv() synchronously, eg

csv().fromString(fs.readFileSync(csvFilePath, 'utf-8')); // (synchronous)

you would still need a way to get the results… The ‘getting the results’ part is all asynchronous, either by stream (asynchonous), event (asynchronous), promise (asynchronous).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async/Await Error Handling - Beginner JavaScript - Wes Bos
We will talk about error handling strategies for async await in this lesson. ... We are using await to get the data from...
Read more >
Error handling with async/await and promises, n² ... - CatchJS
Async await error handling has a bunch of edge cases. Here's the full run down of how to make sense of all the...
Read more >
Async/await - The Modern JavaScript Tutorial
Here's an example with a promise that resolves in 1 second: async function f() { let promise = new Promise((resolve, reject) => {...
Read more >
Easier Error Handling Using async await in JavaScript
I show you an easier way to ensure you don't have to muddy up your async await functions with try catch. The 2...
Read more >
await - JavaScript - MDN Web Docs
The await operator is used to wait for a Promise and get its fulfillment value. It can only be used inside an async...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found