简体   繁体   中英

Phantom.js callback Reference errors when refactoring Phantom.js on Node.js/Express.js to avoid “callback hell”

I have this crazy large block of code that functions, but which I am hoping to refactor properly. With accordance to Callback Hell , I tried to break it down into non-anonymous functions and seperate those functions from the central code.

However, I am running into the problem that a lot of the different sections of the code are dependent on using the others as parameters. The first error message I received in the sequence is ReferenceError: page is not defined

The un-refactored code is:

function startMyFunction(firstLayerUrl) {
        phantom.create(function (ph) {
            ph.createPage(function (page) {
                var main_file=firstLayerUrl
                page.open(main_file, function (status) {
                    var linkArray=[];
                    page.evaluate(function (linkArray) {
                        for (var i=0; i < document.getElementsByTagName('a').length; i++) {
                            linkArray.push(document.getElementsByTagName('a')[i].href)
                        }
                        return linkArray;
                    }
                    , function finished(result) {
                        linkArray = result;
                        runEmailLoop(linkArray);
                        page.close()
                        ph.exit();
                    },linkArray);
                });
            });
        }, {
            dnodeOpts: {
                weak: false
            }
        });
}

The attempted refactored code is:

function runFirstLayer(firstLayerUrl) {
    phantom.create(function (ph) {
        ph.createPage(function (page) {
            var main_file=firstLayerUrl
            page.open(main_file, openIndexPage(status));
        });
    }, {
        dnodeOpts: {
            weak: false
        }
    });
}

function openIndexPage (status) {
    var linkArray=[];
    page.evaluate(evaluatePage(linkArray), finished(result),linkArray);
}

function evaluatePage(linkArray) {
    for (var i=0; i < document.getElementsByTagName('a').length; i++) {
        linkArray.push(document.getElementsByTagName('a')[i].href)
    }
    return linkArray;
}

function finished(result) {
    linkArray = result;
    runEmailLoop(linkArray);
    page.close()
    ph.exit();
}

How can I pass the various dependencies on, such as page , status , since I have tried passing page but then get another error ReferenceError: document is not defined

You need to pass the functions, not call them:

page.open(main_file, openIndexPage);
/* instead of
page.open(main_file, openIndexPage(status));
*/

and

page.evaluate(evaluatePage, finished, linkArray);
/* instead of
page.evaluate(evaluatePage(linkArray), finished(result),linkArray);
*/

There are no status , linkArray , or result values in your code - they are parameters of the function definitions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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