我有一个函数打开一个硒webdriver实例,然后另一个函数,将获取一些信息,然后导航到一个URl的webdriver。我已经使用了一个承诺,因为我被引导认为第二个函数在执行第二个函数之前会等待第一个函数完成。这里是我的参考代码:Javascript承诺不等待异步函数完成
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);
所以这样的代码,我期待的功能2要等到完成功能[1]然而在执行前,这是不是这样的,我误解的承诺?有人能指引我朝着正确的方向吗?在此先感谢
不要使用'新无极()'** **除非你是promisifying一个现有的基于回调的API(请参阅[显式承诺构造反模式](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how- DO-I-避免-吧))。如果您想创建已解决的承诺,请使用'return Promise.resolve('Browser Opened');' – Tomalak
在Selenium的上下文中,请阅读https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs并考虑将您的代码到异步/等待。 – Tomalak