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

134
Views
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 answers
Answer question

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 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!