简体   繁体   English

使用 VSCode 和 Pupeteer 在 REPL 循环中评估 promise

[英]Evaluating promise in REPL loop with VSCode and Pupeteer

在此处输入图像描述

I'm getting started with puppeteer but have minimal node experience.我开始使用 puppeteer,但节点经验很少。 I'm interested in debugging and trying out pieces of code in a REPL loop.我对调试和尝试 REPL 循环中的代码片段很感兴趣。 So far I have the following:到目前为止,我有以下内容:

const puppeteer = require('puppeteer');


(async () => {

const browser = await puppeteer.launch({
headless: false,

});
const page = await browser.newPage();
await page.goto('https:yahoo.com');
 await page.screenshot({path: 'example.png'});

 await browser.close();
})();

I tried to take a second screen shot by entering:我尝试通过输入以下内容进行第二个屏幕截图:

 page.screenshot({path: 'example1.png'});

but this returns a promise.但这会返回 promise。 Is there a way to evaluate the result within the REPL loop有没有办法在 REPL 循环中评估结果

EDIT:编辑:

I entered both lines into the REPL at the bottom of the debug console, the output is in the screenshot.我在调试控制台底部的 REPL 中输入了这两行,output 在屏幕截图中。 Am I doing something wrong?难道我做错了什么?

在此处输入图像描述

EDIT2:编辑2:

I entered your code into the debug window REPL at the bottom of the debug console, the output is in the screenshot.我将您的代码输入到调试控制台底部的调试 window REPL 中,output 在屏幕截图中。

在此处输入图像描述

If you want to pay with the result in the REPL you'd have to do something like this:如果您想用 REPL 中的结果付款,您必须执行以下操作:

var res; page.screenshot({path: 'example1.png'}).then(r => {res=r;console.log('done')});

The done string is printed you'll have the result in your res variable so you can play with it. done的字符串被打印出来,你会在 res 变量中得到结果,这样你就可以使用它了。

Step by Step:一步步:

  • var res is declaring an empty variable so you can use it later var res声明了一个空变量,以便您以后可以使用它

  • page.screenshot({path: 'example1.png'}) will return a promise, hence the .then right after it page.screenshot({path: 'example1.png'})将返回 promise,因此.then紧随其后

  • .then receives a function that will be called asynchronously when the promise is resolved. .then接收 function 将在 promise 解决时异步调用。 That function will be called with some input.将使用一些输入调用 function。

  • r => {res=r;console.log('done')} this is the anonymous function passed to the then of that promise. r => {res=r;console.log('done')}这是匿名的 function 传递给then promise。 The argument passed by page.screenshot(...).then will be stored in the r param/variable. page.screenshot(...).then传递的参数将存储在r参数/变量中。

  • res=r; this sets to the res variable the argument sent from the resolution of the promise.这会将 promise 的分辨率发送的参数设置为res变量。

  • console.log('done') is just there so you will know when the promise is resolved. console.log('done')就在那里,因此您将知道 promise 何时解决。

  • after that, you can inspect the res variable to understand in detail whatever is there.之后,您可以检查res变量以详细了解其中的任何内容。

You can use this approach for ANY type of promise resolution debug, since when in the console (at the moment) we don't have the ability to run something like var res = await page.screenshot({path: 'example1.png'})您可以将此方法用于任何类型的 promise 分辨率调试,因为在控制台中(目前)我们无法运行类似var res = await page.screenshot({path: 'example1.png'})

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

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