Async / Await嵌套问题?代码块未执行

时间:2018-03-02 14:09:32

标签: node.js async-await logic puppeteer

我有一块似乎没有执行的代码。 我在Ubuntu 16.04上使用Node.Js 8.9

const puppet =  require("puppeteer");
const fs = require("fs");

let oldIP = "";
( async () =>{
const browser = await puppet.launch({headless:false});
const Page = await browser.newPage();

try {
await Page.goto("http://192.168.100.1");
await Page.waitFor(1000);

await Page.click("#txt_Username");
await Page.keyboard.type(username);
await(console.log("Entered Username"));

await Page.focus("#txt_Password");
await Page.keyboard.type(passwd);
await(console.log("Entered Password"));

await Page.click("#button");
await(console.log("Authenticating"));
await Page.waitForNavigation("load");
await Page.goto("http://192.168.100.1/html/bbsp/waninfo/waninfo.asp");



let ip = await Page.evaluate(async ()=>{
let address = await document.querySelector("#record_0>td:nth-child(3)").innerText;
await console.log(address);
return address;
});

await console.log("Public(NEW) IP Address is: "+ ip);    

fs.readFileSync("Currentaddress.txt", "utf8", async (err,data)=>{
    if(err) {
        console.log(err);
    }
    console.log("Test:"+oldIP);

    //Why is this code not getting executed?
    if(data){
        oldIP = data.trim();
        await console.log("Old IP is :"+oldIP);
    }


});

await (async ()=>{
    if (ip!==oldIP){
        fs.writeFileSync("Currentaddress.txt", ip, "utf8", async (err,data)=>{
            if(err) console.log(err);   
        });

    await console.log("Old IP 2 is :"+oldIP);   
    await console.log("new Ip written  "+ip);
    await process.exit(1);
    }
    //If they're the same
    await (async ()=>{
    await console.log("No changes Noted");
    await process.exit(1);
    })();

})();

}
//end of try block

catch(error){
    console.log(error);
}
})();

let username = "root";
let passwd = "process.env.PASSWD";

评论//why is this code not getting executed?下的代码块未触发。 我添加了console.log()来检查并且它不会触发。代码运行时没有任何语法错误。

我正在尝试创建一个脚本来检查路由器的公共IP地址。 我已成功获取IP并将其写入txt文件。

但是,阅读文件似乎无法正常工作。

这是我的输出,

Entered Username
Entered Password
Authenticating
Public(NEW) IP Address is: xx.xx.xxx.xx
Old IP 2 is :
new Ip written  xx.xx.xxx.xx

出于安全考虑,我对IP进行了模糊处理,但确实返回了有效的IP地址。

然而,

Old IP 2 is :仍为空白。

我错过了什么?

3 个答案:

答案 0 :(得分:1)

函数fs.readFileSync是同步的,所以试试这个:

try {
  var data = fs.readFileSync("Currentaddress.txt"), {encoding: "utf8"});
  if(data){
    var oldIP = data.trim();
    console.log("Old IP is :"+oldIP);
  }
} catch (error) {
    console.log(err);
}

答案 1 :(得分:0)

readFileSync是同步的。如果你想要异步功能,你应该使用fs.readFile https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback

实际上应该这样做:

fs.readFile("Currentaddress.txt", "utf8", (err,data)=>{
    if(err) {
        console.log(err);
    }
    console.log("Test:"+oldIP);

    //Why is this code not getting executed?
    if(data){
        oldIP = data.trim();
        console.log("Old IP is :"+oldIP);
    }


});

如果你想等待fs.readFile,你应该首先宣传它。

答案 2 :(得分:0)

而不是使 readFileSync 将readFile转换为promise并使用await调用。因为如果你使用readFileSync,它会阻塞线程并停止其他进程,直到readFileSync完成。

使用 util.promisify 将readFile回调转换为承诺。

expectMsg*

使用等待

getLastSender()