Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodejS + PhantomJS warn: exit() was called before waiting for commands to finish. Make sure you are not calling exit() too soon

Try to use PhantomJS with NodeJS to check websites loading time.

But got this message after

phantom_instance.exit();

Already tried to run this line after the Promise return

here are my codes:

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});
        })
});
}

any idea?

Thanks.

like image 334
Wahbigbig Avatar asked Jan 23 '26 05:01

Wahbigbig


1 Answers

I could avoid the mentioned warning by executing var promise = page.close(); and waiting for the promise to resolve before executing phantom_instance.exit().

like image 89
dim Avatar answered Jan 25 '26 04:01

dim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!