Await error when running documentation example
See original GitHub issueI’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:
- Created 5 years ago
- Comments:6 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
As #272 pointed out,
await
can only be used in anasync
function. try this:@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 withBut 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
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).