nodejs 不等待木偶操纵者完成
nodejs not waiting for puppeteer to finish
我在我的代码中设置了以下设置,用户在 node.js 终端中选择一个基本的人偶脚本,然后应该完全执行。完成后,用户应该再次获得 'main menu'。还有一些脚本需要额外的输入。
但是当我执行脚本时,它最初会等待用户输入,但不会等待 puppeteer 脚本结束才返回 'main menu'
我应该如何正确使用异步函数来完成这项工作?我已经查过了,但无法进一步了解它。提前致谢!
示例脚本:
const console = require('console');
const puppeteer = require('puppeteer');
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
async function function1() {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setViewport({ width: 1280, height: 800 })
await page.goto('https://www.nytimes.com/')
await page.screenshot({ path: 'nytimes.png', fullPage: true })
await browser.close()
}
async function function2() {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setViewport({ width: 1280, height: 800 })
await page.goto('https://www.nytimes.com/')
await page.screenshot({ path: 'nytimes.png', fullPage: true })
await browser.close()
}
async function function3(address, location) {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setViewport({ width: 1280, height: 800 })
await page.goto(address)
await page.screenshot({ path: location, fullPage: true })
await browser.close()
}
function Main() {
console.log('What script do you want to use?');
console.log("script 1 -> '1'");
console.log("script 1 -> '2'");
console.log("script 1 -> '3'");
console.log("EXIT -> 'x'");
rl.question('Choice: ', (answer) => {
if (answer == "1")
{
function1().then(Main());
}
else if (answer == "2")
{
function2().then(Main());
}
else if (answer == "3")
{
rl.question('Choice: ', (address) => {
rl.question('Choice: ', (location) => {
function3(address, location).then(Main());
});
});
}
else if (answer == "x")
{
rl.close();
process.exit();
}
else
{
console.log("Pick an option!");
Main();
}
});
}
Main();
在 Promise 链中指定 Main
不带括号:
function Main() {
if (...) {
function1().then(Main) // ✅
}
...
}
或者,Main
可以是一个异步函数:
async function Main() {
if (...) {
await function1()
} else if (...) {
await function2()
} else {
rl.close()
return process.exit()
}
Main()
}
我在我的代码中设置了以下设置,用户在 node.js 终端中选择一个基本的人偶脚本,然后应该完全执行。完成后,用户应该再次获得 'main menu'。还有一些脚本需要额外的输入。
但是当我执行脚本时,它最初会等待用户输入,但不会等待 puppeteer 脚本结束才返回 'main menu'
我应该如何正确使用异步函数来完成这项工作?我已经查过了,但无法进一步了解它。提前致谢!
示例脚本:
const console = require('console');
const puppeteer = require('puppeteer');
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
async function function1() {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setViewport({ width: 1280, height: 800 })
await page.goto('https://www.nytimes.com/')
await page.screenshot({ path: 'nytimes.png', fullPage: true })
await browser.close()
}
async function function2() {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setViewport({ width: 1280, height: 800 })
await page.goto('https://www.nytimes.com/')
await page.screenshot({ path: 'nytimes.png', fullPage: true })
await browser.close()
}
async function function3(address, location) {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setViewport({ width: 1280, height: 800 })
await page.goto(address)
await page.screenshot({ path: location, fullPage: true })
await browser.close()
}
function Main() {
console.log('What script do you want to use?');
console.log("script 1 -> '1'");
console.log("script 1 -> '2'");
console.log("script 1 -> '3'");
console.log("EXIT -> 'x'");
rl.question('Choice: ', (answer) => {
if (answer == "1")
{
function1().then(Main());
}
else if (answer == "2")
{
function2().then(Main());
}
else if (answer == "3")
{
rl.question('Choice: ', (address) => {
rl.question('Choice: ', (location) => {
function3(address, location).then(Main());
});
});
}
else if (answer == "x")
{
rl.close();
process.exit();
}
else
{
console.log("Pick an option!");
Main();
}
});
}
Main();
在 Promise 链中指定 Main
不带括号:
function Main() {
if (...) {
function1().then(Main) // ✅
}
...
}
或者,Main
可以是一个异步函数:
async function Main() {
if (...) {
await function1()
} else if (...) {
await function2()
} else {
rl.close()
return process.exit()
}
Main()
}