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

135
Visualizações
Waiting for another async function to finish before running

I have two functions that I want to run one after the other finishes. I'm using webdriver.IO so I need to wait for one function to log into the page, and then I want another function to run.
Other questions here on SOF are telling me to use a promise, however I need to use a promise, however when I wrap my functions in a promise I get the error SyntaxError: await is only valid in async function.
I have a login function

const Login = async () => {
    const browser = await remote({
        capabilities: {
            browserName: 'chrome'
        }
    })
    const userName = await browser.$('#username')
    const password = await browser.$('#password')
    await userName.setValue(process.env.NAMEUSERNAME)
    await password.setValue(process.env.PASSWORD)
    const loginButton = await browser.$('button[type="submit"]')
    await loginButton.click()
}

Once this completes and the page loads I want to run another function. I have gotten this to work with a setTimeout however I don't want to just have a chain of setTimeout in my application

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's hard to give you precise code without seeing where this is called from: but presuming it's inside some other function, you just have to mark that one async and use await inside it:

async theOuterFunction() {
    await Login();
    doTheOtherThing();
}

(If doTheOtherThing also returns a promise you could await that too - and would need to if you want to run other code inside theOuterFunction after that. But there's no need if it's the last statement inside the function.)

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