0

I am a node.js beginner and I'm trying to check status of domains I read from a csv file. All works fine, however when the script reaches the end it simply hangs and does not quit. Below is the code I'm working with. What am i missing here? Any help would be greatly appreciated!

```
const fs = require('fs');
const request = require('request');
let https = require('https');

const input_path = 'Path_to_csv'



function FetchUrls (callback) {

     fs.readFile(input_path, 'utf8', function (err, data) {

     let dataArray = data.split(/\r?\n/);

     console.log(`loaded ${dataArray.length} items`)

     callback(dataArray)});

     }


function getData (dataArray) {

     let urls = dataArray

     for (let url of urls) {

         https.get(url, function(res) {

         if(res.statusCode == 500) {

            console.log("Domain Down")

         } else {

            console.log("Domain Up")
         }

         }).on('error', function(e) {

             console.log(e)
             process.exit()

    });

   }
}


FetchUrls(function(dataArray) {
getData(dataArray)

})
```
Matthew
  • 411
  • 6
  • 22
  • 2
    Does this answer your question? [How to return value from an asynchronous callback function?](https://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) – marekful Mar 03 '20 at 16:17
  • that definitely helps! thanks – Matthew Mar 04 '20 at 15:36

1 Answers1

0

You need to add a return after the for loop. But I would wrap it in a promise like:

FetchUrls(input_path, function(dataArray) {
  let promises = []
  for (let domain of dataArray) {
    promises.push(new Promise((resolve, reject) => {
      // ... do the https.getting things, and then resolve() where you think it should end
    }))
  }
   Promise.all(promises).then( Nothing => return Nothing)
   .catch(error => return error)
}
Balázs Zákány
  • 407
  • 2
  • 11
  • Unfortunately I keep getting the same behaviour when I add this – Matthew Mar 04 '20 at 16:44
  • I've updated the code. I've been playing with it a bit so its quite different from the original one but the process is still the same – Matthew Mar 04 '20 at 20:59