简体   繁体   English

VO,生成器,Promise和返回对象之间的交互。

[英]Interaction of VO, Generators, Promises and return object.

I'm trying to understand how nightmare and vo interact. 我想了解噩梦和vo如何相互作用。 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 为什么生成器函数需要yield和return语句才能使代码起作用
  • how the yields get automatically triggered by vo / nightmare in order to make the sequence work reasonably. vo /噩梦如何自动触发收益以使序列合理运行。 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. 背景是,我想知道何时将收益放置在什么地方,但是这里的yield语句似乎除了实际触发等待之外没有做任何其他事情。 With my current knowledge, I can just copy the example and "wait" when calling ".goto()" and ".end()". 以我目前的知识,我可以在调用“ .goto()”和“ .end()”时复制示例并“等待”。

Unfortunately, nightmare documentation doesn't say how its design works (interaction with promises and generators). 不幸的是,噩梦文档没有说明其设计如何工作(与Promise和生成器的交互)。

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. 如果有人想到一个示例,如何手动解决run()承诺并触发所有使生成器函数按预期工作所必需的.next(),那么这将是一个很大的帮助。

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. Vo将在运行时调用该方法。 The code should read 该代码应阅读

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

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

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