This function scrapes data from a webpage by spawning a process that executes a CasperJS web scraping script. The spawned process outputs data to stdout. This function has an event listener for stdout and this is how the parent process (Node) gets the scraped data back. How can I make this more modular? Also, would creating a child_process class make more sense?function scrapeLinks(location, callback) {    // stores any data emitted from the stdout stream of spawned casper process     var processData = ;     // stores any errors emitted from the stderror stream of spawned casper process     var processError = ;    // initialises casperjs link scraping script as spawned process    var linkScrapeChild = child_process.spawn(casperjsPath, ['casperLinkScript.js ' + location]);     linkScrapeChild.stdout.on('data', function onScrapeProcessStdout(data) {         processData += data.toString();         console.log(data.toString())     });     linkScrapeChild.stderr.on('data', function onScrapeProcessError(err) {         processError += err.toString();     });     linkScrapeChild.on(error, function onScrapeProcessError(err) {         processError = err.toString();     });    //once spawned casper process finishes execution call the callback    linkScrapeChild.on('close', function onScrapeProcessExit(code) {        console.log('Child process - Location Scrape:  ' + location + ' - closed with code: ' + code);         processData = convertToArray(processData);         // filter out non valid listing links         listingLinks = filterLinks(processData);         //console.log(listingLinks);         // filter duplicates         var uniqueLinks = [ ...new Set(listingLinks) ];        if(uniqueLinks.length === 0){            processError += 'No valid listings found for ' + location        }         logScrapeResults(processError, uniqueLinks, location);         callback(processError || null, uniqueLinks);    });}
Spawning child processes and event-listening
javascript;node.js;child process