Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda