0

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

  • Do not use `new Promise()` **unless** you a promisifying an existing, callback-based API (see the [explicit promise construction antipattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it)). If you want to create a resolved promise, use `return Promise.resolve('Browser Opened');` – Tomalak Oct 12 '17 at 18:27
  • In the context of Selenium, read https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs and consider moving your code to async/await. – Tomalak Oct 12 '17 at 18:30

2 Answers2

2

driver is not returned 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);
guest271314
  • 1
  • 15
  • 104
  • 177
0

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);
Nick
  • 16,066
  • 3
  • 16
  • 32
  • @HarveyLewis ah ok, in that case it's a scoping issue. Function2 has no reference to `driver`. – Nick Oct 12 '17 at 18:28