0

I am reading a CSV file using csv-parser npm module createReadStream.

export default async function main() {
  const readstream = fs.createReadStream('src/working_file.csv');
stream.on('data', data => {
const val = await fun(param1, param2, param3);
   });
  fun(param1, param2, param3){
    return true;
  }
}

I have to call function fun with await but it is throwing me the error.await is only valid in async function. Can anyone help me how to fix this?

2 Answers 2

2

Just indicate that the callback function is async.

stream.on('data', async data => {
  // Do async stuff
})
Sign up to request clarification or add additional context in comments.

1 Comment

each data has a JSON object and I have written a function call in call back function. Which is calling by all the objects at once. Can you tell me why? and I want to call the function in callback function one after once.
0

The function that you await must return a promise.

export default async function main() {
  const readstream = fs.createReadStream('src/working_file.csv');
stream.on('data', async data => {
   try{
      const val = await fun(param1, param2, param3);
    } catch(err) {
    //handle error
    }

   });
  fun(param1, param2, param3){
    return new Promise((resolve, reject)=>{
         resolve(some_value)
    })
  }
}

2 Comments

It needs to be specified as async and that very change means it does return a promise, by definition. An expression that is awaited is wrapped in a promise meaning that anything may be awaited
I've tried this and it did not work for me. See this link for more details: stackoverflow.com/questions/63308963/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.