简体   繁体   中英

Javascript promise not waiting for async function to complete

I have a function that opens a selenium webdriver instance, and then another functions that will grab some info and then navigate the webdriver to a URl. I have used a promise as I was led to believe the second function would wait for the first to complete before executing the second. Here is my code for reference:

function function1(){
  let driver = new Builder()
  .forBrowser('firefox')
  .build();
    driver.get(url);

  return new Promise((resolve, reject)=>{
    resolve('Browser Opened')
  })

 }

function function2(){
  ** code to create URL **
  driver.get(urlCreated);
}

function1().then(function2);

So this is the code, i was expecting function 2 to wait until function 1 to be completed before executed however this is not the case, am I misunderstanding promises? Can someone point me in the right direction? Thanks in advance

driver is not return ed from function1

function function1() {
  return new Promise((resolve, reject)=>{
    let driver = new Builder()
    .forBrowser('firefox')
    .build();
    driver.get(url);
    resolve(driver);
  })
}

function function2(driver) {
  driver.get(urlCreated);
}

function1().then(function2);

You're missing a closing } on function1

function function1(){
  let driver = new Builder()
  .forBrowser('firefox')
  .build();
    driver.get(url);

  return new Promise((resolve, reject)=>{
    resolve('Browser Opened')
  })
}

function function2(){
  ** code to create URL **
  driver.get(urlCreated);
}

function1().then(function2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM