简体   繁体   中英

Interaction of VO, Generators, Promises and return object.

I'm trying to understand how nightmare and vo interact. Please find below the (almost) original example of the nightmare page.

Can anyone please give me a hint how to find out the sequence of things which happen, ie

  • why the generator function needs yield and return statements for the code to work
  • how the yields get automatically triggered by vo / nightmare in order to make the sequence work reasonably. Background is, I'd like to know when to place yields where, however the yield statements here don't seem to do anything else than actually trigger a wait. With my current knowledge, I can just copy the example and "wait" when calling ".goto()" and ".end()".

Unfortunately, nightmare documentation doesn't say how its design works (interaction with promises and generators).

If anybody can think of an example how to manually resolve the run() promise and trigger all of the .next() which are necessary to make the generator function work as intended, this would already be a big help.

var Nightmare = require('nightmare');
var vo = require('vo');

vo( run() )(function(err, result) {
    if (err) throw err;
    console.log("end result length: ", result.length);
 });

function *run() {
    var x = Date.now();
    var nightmare = Nightmare();
    var html = yield nightmare
      .goto('http://google.com')
      .evaluate(function() {
        return document.getElementsByTagName('html')[0].innerHTML;
      });

   console.log("done in " + (Date.now()-x) + "ms");
   console.log("result:", html.length);

   yield nightmare.end();
   return html;
}

Thanks !

You need to pass the method in by name and not actually call it. Vo will call the method when it runs. The code should read

vo( run )(function(err, result) {
  if (err) throw err;
  console.log("end result length: ", result.length);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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