How to seperate function with await The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceIs there an “exists” function for jQuery?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?var functionName = function() vs function functionName() How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?How do I return the response from an asynchronous call?

First use of “packing” as in carrying a gun

Why did all the guest students take carriages to the Yule Ball?

Segmentation fault output is suppressed when piping stdin into a function. Why?

What are these Gizmos at Izaña Atmospheric Research Center in Spain?

system() function string length limit

High Q peak in frequency response means what in time domain?

How can I protect witches in combat who wear limited clothing?

What is this lever in Argentinian toilets?

Typeface like Times New Roman but with "tied" percent sign

He got a vote 80% that of Emmanuel Macron’s

How is simplicity better than precision and clarity in prose?

Searching for a differential characteristic (differential cryptanalysis)

How many people can fit inside Mordenkainen's Magnificent Mansion?

How to copy the contents of all files with a certain name into a new file?

Why can't devices on different VLANs, but on the same subnet, communicate?

I could not break this equation. Please help me

What aspect of planet Earth must be changed to prevent the industrial revolution?

Working through the single responsibility principle (SRP) in Python when calls are expensive

When did F become S in typeography, and why?

The following signatures were invalid: EXPKEYSIG 1397BC53640DB551

Derivation tree not rendering

Is there a writing software that you can sort scenes like slides in PowerPoint?

Who or what is the being for whom Being is a question for Heidegger?

How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time



How to seperate function with await



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceIs there an “exists” function for jQuery?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?var functionName = function() vs function functionName() How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?How do I return the response from an asynchronous call?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I just switch from php into full time js. I try to clean code in javascript function but still confuse how to deal with it



for(let i in allEmpData) 
let emp = allEmpData[i]
if(empData == null)
const addEmp = new Employee()
this.addEmpFn(addEmp, emp)

if(empData != null)
this.addEmpFn(empData, emp)




I call function addEmpFn because I put create/update code in thier here is my function addEmpFn



 addEmpFn(empData, emp) 
empData.name = emp.name
return await empData.save()



the error said :



Unexpected token return await



I want to do something like this but In the clean code , seperate function



 for(let i in allEmpData) 
const empData = await Employee.query()
.where('id',1)
.first();
let emp = allEmpData[i]
if(empData == null)
const addEmp = new Employee()
addEmp.name = emp.name
return await empData.save()

if(empData != null)
empData.name = emp.name
return await empData.save()











share|improve this question




























    0















    I just switch from php into full time js. I try to clean code in javascript function but still confuse how to deal with it



    for(let i in allEmpData) 
    let emp = allEmpData[i]
    if(empData == null)
    const addEmp = new Employee()
    this.addEmpFn(addEmp, emp)

    if(empData != null)
    this.addEmpFn(empData, emp)




    I call function addEmpFn because I put create/update code in thier here is my function addEmpFn



     addEmpFn(empData, emp) 
    empData.name = emp.name
    return await empData.save()



    the error said :



    Unexpected token return await



    I want to do something like this but In the clean code , seperate function



     for(let i in allEmpData) 
    const empData = await Employee.query()
    .where('id',1)
    .first();
    let emp = allEmpData[i]
    if(empData == null)
    const addEmp = new Employee()
    addEmp.name = emp.name
    return await empData.save()

    if(empData != null)
    empData.name = emp.name
    return await empData.save()











    share|improve this question
























      0












      0








      0








      I just switch from php into full time js. I try to clean code in javascript function but still confuse how to deal with it



      for(let i in allEmpData) 
      let emp = allEmpData[i]
      if(empData == null)
      const addEmp = new Employee()
      this.addEmpFn(addEmp, emp)

      if(empData != null)
      this.addEmpFn(empData, emp)




      I call function addEmpFn because I put create/update code in thier here is my function addEmpFn



       addEmpFn(empData, emp) 
      empData.name = emp.name
      return await empData.save()



      the error said :



      Unexpected token return await



      I want to do something like this but In the clean code , seperate function



       for(let i in allEmpData) 
      const empData = await Employee.query()
      .where('id',1)
      .first();
      let emp = allEmpData[i]
      if(empData == null)
      const addEmp = new Employee()
      addEmp.name = emp.name
      return await empData.save()

      if(empData != null)
      empData.name = emp.name
      return await empData.save()











      share|improve this question














      I just switch from php into full time js. I try to clean code in javascript function but still confuse how to deal with it



      for(let i in allEmpData) 
      let emp = allEmpData[i]
      if(empData == null)
      const addEmp = new Employee()
      this.addEmpFn(addEmp, emp)

      if(empData != null)
      this.addEmpFn(empData, emp)




      I call function addEmpFn because I put create/update code in thier here is my function addEmpFn



       addEmpFn(empData, emp) 
      empData.name = emp.name
      return await empData.save()



      the error said :



      Unexpected token return await



      I want to do something like this but In the clean code , seperate function



       for(let i in allEmpData) 
      const empData = await Employee.query()
      .where('id',1)
      .first();
      let emp = allEmpData[i]
      if(empData == null)
      const addEmp = new Employee()
      addEmp.name = emp.name
      return await empData.save()

      if(empData != null)
      empData.name = emp.name
      return await empData.save()








      javascript node.js adonis.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 6:46









      TryHardTryHard

      696




      696






















          4 Answers
          4






          active

          oldest

          votes


















          4














          You need to assign function as async as follows:



          async addEmpFn(empData, emp) 
          empData.name = emp.name
          return await empData.save()






          share|improve this answer























          • why you need an async await here?

            – AZ_
            Mar 22 at 7:07


















          3














          await keyword only works inside async functions. so you have to write async keyword before function declaration.



          like this;



          async addEmpFn(empData, emp) 
          empData.name = emp.name
          return await empData.save()






          share|improve this answer























          • thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

            – TryHard
            Mar 22 at 6:56


















          2














          You don't really need an async await here, async await is needed when you want to wait for the promise to get resolved.



          And also return await is an anti-pattern in JS until you don't want to use try-catch around.



          Following code is the way to go, works same as the accepted answer.






           addEmpFn(empData, emp) 
          empData.name = emp.name
          return empData.save()








          share|improve this answer


















          • 1





            Thank you, I just wanted to write the same thing.

            – Woozar
            Mar 22 at 9:03


















          2














          Try to mention async before your funtion like below I mentioned



          async addEmpFn(empData, emp) 
          empData.name = emp.name;
          return await empData.save();






          share|improve this answer

























          • thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

            – TryHard
            Mar 22 at 6:56






          • 1





            Cool.. you come up with problem is important...happy coding

            – umer sayyed
            Mar 22 at 6:58











          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%2f55294231%2fhow-to-seperate-function-with-await%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You need to assign function as async as follows:



          async addEmpFn(empData, emp) 
          empData.name = emp.name
          return await empData.save()






          share|improve this answer























          • why you need an async await here?

            – AZ_
            Mar 22 at 7:07















          4














          You need to assign function as async as follows:



          async addEmpFn(empData, emp) 
          empData.name = emp.name
          return await empData.save()






          share|improve this answer























          • why you need an async await here?

            – AZ_
            Mar 22 at 7:07













          4












          4








          4







          You need to assign function as async as follows:



          async addEmpFn(empData, emp) 
          empData.name = emp.name
          return await empData.save()






          share|improve this answer













          You need to assign function as async as follows:



          async addEmpFn(empData, emp) 
          empData.name = emp.name
          return await empData.save()







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 6:51









          Shubham ChaudharyShubham Chaudhary

          36917




          36917












          • why you need an async await here?

            – AZ_
            Mar 22 at 7:07

















          • why you need an async await here?

            – AZ_
            Mar 22 at 7:07
















          why you need an async await here?

          – AZ_
          Mar 22 at 7:07





          why you need an async await here?

          – AZ_
          Mar 22 at 7:07













          3














          await keyword only works inside async functions. so you have to write async keyword before function declaration.



          like this;



          async addEmpFn(empData, emp) 
          empData.name = emp.name
          return await empData.save()






          share|improve this answer























          • thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

            – TryHard
            Mar 22 at 6:56















          3














          await keyword only works inside async functions. so you have to write async keyword before function declaration.



          like this;



          async addEmpFn(empData, emp) 
          empData.name = emp.name
          return await empData.save()






          share|improve this answer























          • thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

            – TryHard
            Mar 22 at 6:56













          3












          3








          3







          await keyword only works inside async functions. so you have to write async keyword before function declaration.



          like this;



          async addEmpFn(empData, emp) 
          empData.name = emp.name
          return await empData.save()






          share|improve this answer













          await keyword only works inside async functions. so you have to write async keyword before function declaration.



          like this;



          async addEmpFn(empData, emp) 
          empData.name = emp.name
          return await empData.save()







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 6:52









          Özgün ÖzenÖzgün Özen

          511




          511












          • thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

            – TryHard
            Mar 22 at 6:56

















          • thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

            – TryHard
            Mar 22 at 6:56
















          thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

          – TryHard
          Mar 22 at 6:56





          thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

          – TryHard
          Mar 22 at 6:56











          2














          You don't really need an async await here, async await is needed when you want to wait for the promise to get resolved.



          And also return await is an anti-pattern in JS until you don't want to use try-catch around.



          Following code is the way to go, works same as the accepted answer.






           addEmpFn(empData, emp) 
          empData.name = emp.name
          return empData.save()








          share|improve this answer


















          • 1





            Thank you, I just wanted to write the same thing.

            – Woozar
            Mar 22 at 9:03















          2














          You don't really need an async await here, async await is needed when you want to wait for the promise to get resolved.



          And also return await is an anti-pattern in JS until you don't want to use try-catch around.



          Following code is the way to go, works same as the accepted answer.






           addEmpFn(empData, emp) 
          empData.name = emp.name
          return empData.save()








          share|improve this answer


















          • 1





            Thank you, I just wanted to write the same thing.

            – Woozar
            Mar 22 at 9:03













          2












          2








          2







          You don't really need an async await here, async await is needed when you want to wait for the promise to get resolved.



          And also return await is an anti-pattern in JS until you don't want to use try-catch around.



          Following code is the way to go, works same as the accepted answer.






           addEmpFn(empData, emp) 
          empData.name = emp.name
          return empData.save()








          share|improve this answer













          You don't really need an async await here, async await is needed when you want to wait for the promise to get resolved.



          And also return await is an anti-pattern in JS until you don't want to use try-catch around.



          Following code is the way to go, works same as the accepted answer.






           addEmpFn(empData, emp) 
          empData.name = emp.name
          return empData.save()








           addEmpFn(empData, emp) 
          empData.name = emp.name
          return empData.save()





           addEmpFn(empData, emp) 
          empData.name = emp.name
          return empData.save()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 7:06









          AZ_AZ_

          917310




          917310







          • 1





            Thank you, I just wanted to write the same thing.

            – Woozar
            Mar 22 at 9:03












          • 1





            Thank you, I just wanted to write the same thing.

            – Woozar
            Mar 22 at 9:03







          1




          1





          Thank you, I just wanted to write the same thing.

          – Woozar
          Mar 22 at 9:03





          Thank you, I just wanted to write the same thing.

          – Woozar
          Mar 22 at 9:03











          2














          Try to mention async before your funtion like below I mentioned



          async addEmpFn(empData, emp) 
          empData.name = emp.name;
          return await empData.save();






          share|improve this answer

























          • thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

            – TryHard
            Mar 22 at 6:56






          • 1





            Cool.. you come up with problem is important...happy coding

            – umer sayyed
            Mar 22 at 6:58















          2














          Try to mention async before your funtion like below I mentioned



          async addEmpFn(empData, emp) 
          empData.name = emp.name;
          return await empData.save();






          share|improve this answer

























          • thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

            – TryHard
            Mar 22 at 6:56






          • 1





            Cool.. you come up with problem is important...happy coding

            – umer sayyed
            Mar 22 at 6:58













          2












          2








          2







          Try to mention async before your funtion like below I mentioned



          async addEmpFn(empData, emp) 
          empData.name = emp.name;
          return await empData.save();






          share|improve this answer















          Try to mention async before your funtion like below I mentioned



          async addEmpFn(empData, emp) 
          empData.name = emp.name;
          return await empData.save();







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 22 at 7:23









          Tushar Walzade

          2,31931935




          2,31931935










          answered Mar 22 at 6:53









          umer sayyedumer sayyed

          562




          562












          • thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

            – TryHard
            Mar 22 at 6:56






          • 1





            Cool.. you come up with problem is important...happy coding

            – umer sayyed
            Mar 22 at 6:58

















          • thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

            – TryHard
            Mar 22 at 6:56






          • 1





            Cool.. you come up with problem is important...happy coding

            – umer sayyed
            Mar 22 at 6:58
















          thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

          – TryHard
          Mar 22 at 6:56





          thank you, It's work but too many people give me answer. I've to mark the 1st. I very thank you for your help

          – TryHard
          Mar 22 at 6:56




          1




          1





          Cool.. you come up with problem is important...happy coding

          – umer sayyed
          Mar 22 at 6:58





          Cool.. you come up with problem is important...happy coding

          – umer sayyed
          Mar 22 at 6:58

















          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%2f55294231%2fhow-to-seperate-function-with-await%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







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript