Using nodeJS.
I have a main file that calls a web scraper that goes out to a few links and gets raw data from the sites and then the next line calls a function from the web scraper file that returns a JSON object to the main file. But because it takes a little while to go out and scrape the websites the return is returning an empty object. How do you use callbacks or other methods to wait for the data before calling the get method.
main file
var test = require('web-scraper.js');
test.scrape(next);
function next()
{
var json = test.getJSON();
}
web-scraper.js (It uses module exports and is in the node modules folder)
module.exports =
{
scrape: function(callback)
{
startScraper(callback);
},
getJSON: function()
{
return JSON;
}
};
function startScraper(callback)
{
// Does web scraper stuff
callback();
}
You can use JavaScript callback or promises to wait till fetch is completed. Read following article how to do simple http call. It described callback
https://www.google.lk/amp/s/davidwalsh.name/nodejs-http-request/amp?client=ms-android-xiaomi
Read following post to understand how to use promises
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise