javascript - Phantom.js callback Reference errors when refactoring Phantom.js on Node.js/Express.js to avoid "callback hell" -



javascript - Phantom.js callback Reference errors when refactoring Phantom.js on Node.js/Express.js to avoid "callback hell" -

i have crazy big block of code functions, hoping refactor properly. accordance callback hell, tried break downwards non-anonymous functions , seperate functions central code.

however, running problem lot of different sections of code dependent on using others parameters. first error message received in sequence referenceerror: page 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) { (var i=0; < document.getelementsbytagname('a').length; i++) { linkarray.push(document.getelementsbytagname('a')[i].href) } homecoming 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) { (var i=0; < document.getelementsbytagname('a').length; i++) { linkarray.push(document.getelementsbytagname('a')[i].href) } homecoming linkarray; } function finished(result) { linkarray = result; runemailloop(linkarray); page.close() ph.exit(); }

how can pass various dependencies on, such page, status, since have tried passing page error referenceerror: document not defined

you need pass functions, not phone 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 no status, linkarray, or result values in code - parameters of function definitions.

javascript jquery node.js

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -