Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Where to put await in async function

I am trying to use an async call however whenever I put the await command word in, the script fails to run. Any suggestions gratefully accepted.

alternative code at bottom based on comment provided

    async function masterDeploy(PrimaryControl){
        //2. Collect learning event dates from programme
        LearnEvents = progLearnerEvent(progGuid)                                                                                
          .then(LearnEvents =>{
            console.log(LearnEvents)                                                                    
            for (var b = 0; b < LearnEvents.length; b++) {                                          
              retLearnEvents(LearnEvents[b],progGuid,b) //need to wait for these results before moving to next loop, code does not run if await placed at beginning of this line
              console.log(FlightPlanArray)  
              learnEventsArrComb.push(FlightPlanArray)
              console.log(learnEventsArrComb)
            }
            return learnEventsArrComb
          })
          .then(learnEventsArrComb => {
            console.log(learnEventsArrComb)
          })
          .catch(error =>
              {DisplayError(error)}
            );

alternative starts here

        LearnEvents = progLearnerEvent(progGuid)    
            await (LearnEvents =>{
            //.then(LearnEvents =>{
              console.log(LearnEvents)                                                                  
              for (var b = 0; b < LearnEvents.length; b++) {                                            
                await retLearnEvents(LearnEvents[b],progGuid,b) //need to wait for these results before moving to next loop, code does not run if placed at beginning of this line
                console.log(FlightPlanArray)    
                learnEventsArrComb.push(FlightPlanArray)
                console.log(learnEventsArrComb)
              }
              return learnEventsArrComb
            })
            //.then(learnEventsArrComb => {
            //  console.log(learnEventsArrComb)
            //})
            //.catch(error =>
            //      {DisplayError(error)}
            //);
      
      }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you can do it like this:

const masterDeploy = async (PrimaryControl) =>{
        //2. Collect learning event dates from programme
        LearnEvents = await progLearnerEvent(progGuid)                                                                                
          .then(LearnEvents =>{
            console.log(LearnEvents)                                                                    
            for (var b = 0; b < LearnEvents.length; b++) {                                          
             await retLearnEvents(LearnEvents[b],progGuid,b) //need to wait for these results before moving to next loop, code does not run if await placed at beginning of this line
              console.log(FlightPlanArray)  
              learnEventsArrComb.push(FlightPlanArray)
              console.log(learnEventsArrComb)
            }
            return learnEventsArrComb
          })
          .then(learnEventsArrComb => {
            console.log(learnEventsArrComb)
          })
          .catch(error =>
              {DisplayError(error)}
            );

try this instead...

about 4 years ago · Juan Pablo Isaza Report

0

You need to add await when you call to masterDeploy func

function main(){ 
  await masterDeploy(info)
}

also, I recommend to do these changes in your code:

async function masterDeploy(PrimaryControl){
//2. Collect learning event dates from programme
try{
    const LearnEvents = await progLearnerEvent(progGuid)  
    console.log(LearnEvents)                                                                              
    for (let b = 0; b < LearnEvents.length; b++) {                                                    retLearnEvents(LearnEvents[b],progGuid,b) //need to wait for these results before moving to next loop, code does not run if await placed at beginning of this line
        console.log(FlightPlanArray)  
        learnEventsArrComb.push(FlightPlanArray)
        console.log(learnEventsArrComb)
    }
}
catch(error){
    DisplayError(error)
}
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!