Node.js Asynchronous Library Comparison - Q vs Async

前端 未结 3 1335
梦谈多话
梦谈多话 2020-12-08 07:35

I have used kriskowal\'s Q library for a project (web scraper / human-activity simulator) and have become acquainted with promises, returning them and resolving/rejecting th

3条回答
  •  -上瘾入骨i
    2020-12-08 07:58

    While this is still not an actual answer to my question (Q vs async), regarding my problem, I've found Selenium / WebDriverJs to be a viable solution.

    driver.get('http://www.google.com');
    driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
    driver.findElement(webdriver.By.name('btnG')).click();
    driver.wait(function() {
      return driver.getTitle().then(function(title) {
        return title === 'webdriver - Google Search';
      });
    }, 1000);
    

    WebDriver uses a queue to execute promises sequentially, which helps immensely with controlling indentation. Its promises are also compatible with Q's.

    Creating a sequence of promises is no longer an issue. A simple for-loop will do.

    As for stopping early in a sequence, don't do this. Instead of using a sequence, use an asynchronous-while design and branch.

提交回复
热议问题