how to call async function in .on event nodejs










-1















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?










share|improve this question


























    -1















    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?










    share|improve this question
























      -1












      -1








      -1








      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?










      share|improve this question














      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?







      javascript node.js events






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 13:05









      m9m9mm9m9m

      475512




      475512






















          2 Answers
          2






          active

          oldest

          votes


















          2














          Just indicate that the callback function is async.



          stream.on('data', async data => 
          // Do async stuff
          )





          share|improve this answer























          • 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.

            – m9m9m
            Nov 14 '18 at 4:58


















          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)
          )







          share|improve this answer




















          • 1





            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

            – Aluan Haddad
            Nov 13 '18 at 13:27











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281665%2fhow-to-call-async-function-in-on-event-nodejs%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Just indicate that the callback function is async.



          stream.on('data', async data => 
          // Do async stuff
          )





          share|improve this answer























          • 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.

            – m9m9m
            Nov 14 '18 at 4:58















          2














          Just indicate that the callback function is async.



          stream.on('data', async data => 
          // Do async stuff
          )





          share|improve this answer























          • 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.

            – m9m9m
            Nov 14 '18 at 4:58













          2












          2








          2







          Just indicate that the callback function is async.



          stream.on('data', async data => 
          // Do async stuff
          )





          share|improve this answer













          Just indicate that the callback function is async.



          stream.on('data', async data => 
          // Do async stuff
          )






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 13:07









          EnslevEnslev

          39517




          39517












          • 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.

            – m9m9m
            Nov 14 '18 at 4:58

















          • 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.

            – m9m9m
            Nov 14 '18 at 4:58
















          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.

          – m9m9m
          Nov 14 '18 at 4:58





          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.

          – m9m9m
          Nov 14 '18 at 4:58













          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)
          )







          share|improve this answer




















          • 1





            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

            – Aluan Haddad
            Nov 13 '18 at 13:27
















          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)
          )







          share|improve this answer




















          • 1





            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

            – Aluan Haddad
            Nov 13 '18 at 13:27














          0












          0








          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)
          )







          share|improve this answer















          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)
          )








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 13:24

























          answered Nov 13 '18 at 13:19









          squeekyDavesqueekyDave

          392114




          392114







          • 1





            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

            – Aluan Haddad
            Nov 13 '18 at 13:27













          • 1





            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

            – Aluan Haddad
            Nov 13 '18 at 13:27








          1




          1





          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

          – Aluan Haddad
          Nov 13 '18 at 13:27






          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

          – Aluan Haddad
          Nov 13 '18 at 13:27


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281665%2fhow-to-call-async-function-in-on-event-nodejs%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto