Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda