NodejS + PhantomJS 警告:exit() 在等待命令完成之前被调用。确保您没有过早地调用 exit()

Wah*_*big 3 node.js phantomjs

尝试使用 PhantomJS 和 NodeJS 来检查网站加载时间。

但收到这条消息后

phantom_instance.exit();
Run Code Online (Sandbox Code Playgroud)

Promise 返回后已经尝试运行此行

这是我的代码:

function timer(url) {
return new Promise(function(resolve, reject) {
    let phantom_instance = null;
    let sitepage = null;
    let start_time = null;
    let end_time = null;
    let interval_obj = null;
    let loading_time = 0;
    phantom.create()
        .then(function(instance) {
            phantom_instance = instance;
            return phantom_instance.createPage();
        })
        .then(function(page) {
            sitepage = page;
            page.property('viewportSize', {
                width: 1280,
                height: 1024
            })
            start_time = Date.now();
            return page.open(url);
        })
        .then(function(status) {
            return new Promise(function(resolve, reject) {
                if (status == 'success') {
                    interval_obj = setInterval(function() {
                        //tell the browser to return document.readyStatue
                        sitepage.evaluate(function() {
                            return document.readyState;
                        })
                        .then(function(ready_state) {
                            if (ready_state == "complete") {
                                end_time = Date.now();
                                sitepage.close();
                                clearInterval(interval_obj);
                                loading_time = end_time - start_time;
                                return resolve(loading_time);
                            } else {
                                console.log(ready_state);
                            }
                        })
                    }, 1000);
                } else {
                    //phantom_instance.exit();
                    return reject(status);
                }
            });
        })
        .then(function(loading_time) {
            console.log("[Speed Tester] Completed in : " + loading_time);
            phantom_instance.exit();
            return resolve({time:loading_time});
        })
});
}
Run Code Online (Sandbox Code Playgroud)

任何的想法?

谢谢。

小智 5

我可以通过var promise = page.close();在执行之前执行并等待承诺解决来避免提到的警告phantom_instance.exit()