0

Can someone explain how this code works? I was researching on how I could make loops to scrape with nightmareJS and found this, it works but i just cannot understand how does it work or how it could be written without generators.

var Nightmare = require('nightmare');
var vo = require('vo');
vo(run)(function(err, result) {
if (err) throw err;
});

function* run() {
var nightmare = Nightmare(),
MAX_PAGE = 10,
currentPage = 0,
nextExists = true,
links = [];

yield nightmare
    .goto('https://www.yahoo.com')
    .type('#uh-search-box', 'github nightmare')
    .click('#uh-search-button')
    .wait('ol.searchCenterMiddle')


nextExists = yield nightmare.visible('.next');

while (nextExists && currentPage < MAX_PAGE) {
    links.push(yield nightmare
        .evaluate(function() {
            var links = document.querySelectorAll("ol.searchCenterMiddle a");
            console.log(links[0].href);
            return links[0].href;
        }));

        yield nightmare
            .click('.next')
            .wait('body')

        currentPage++;
        nextExists = yield nightmare.visible('.next');
}
console.dir(links);
yield nightmare.end();
}
Caio Fontes
  • 1,973
  • 2
  • 12
  • 12
  • 1
    You can use `Promise` and `for..of` loop or `async/await` to achieve same result, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield, https://stackoverflow.com/questions/43547606/implementing-coroutine-control-flow-in-javascript/ – guest271314 Sep 25 '17 at 07:07
  • 1
    I can strongly recommend this article series https://davidwalsh.name/es6-generators/ – Mertcan Diken Sep 25 '17 at 14:14

0 Answers0