繁体   English   中英

异步匿名函数:then()是否必要?

[英]Async anonymous function: is then() necessary?

请参阅以下代码

'use strict';

const {Builder, By, Key, until} = require('..');
const {Options} = require('../chrome');

(async function() {
  let driver;
  try {
    driver = await new Builder()
        .forBrowser('chrome')
        .setChromeOptions(new Options().androidChrome())
        .build();
    await driver.get('http://www.google.com/ncr');
    await driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN);
    await driver.wait(until.titleIs('webdriver - Google Search'), 1000);
  } finally {
    await driver && driver.quit();
  }
})().then(_ => console.log('SUCCESS'), err => console.error('ERROR: ' + err));

我的问题是:什么时候会调用.then(_ ... ?异步函数不会返回任何内容;此外,它似乎捕获了所有可能的问题,但直到最后才“最终”。 .then(在那里,以便它会捕获退出驱动程序的错误?还是还有更多?

当您在函数中引发错误时,将调用错误处理程序。

 (async function() { throw 1; })() .then(_ => console.log('SUCCESS'), err => console.error('ERROR: ' + err)) 

此外,您不需要try-catch即可使用完整功能块。 这就是您中的第二个参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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