I am a total scrub with the node http module and having some trouble.

The ultimate goal here is to take a huge list of urls, figure out which are valid and then scrape those pages for certain data. So step one is figuring out if a URL is valid and this simple exercise is baffling me.

say we have an array allURLs:

```
["www.yahoo.com", "www.stackoverflow.com", "www.sdfhksdjfksjdhg.net"]

```

The goal is to iterate this array, make a get request to each and if a response comes in, add the link to a list of workingURLs (for now just another array), else it goes to a list brokenURLs.

```
var workingURLs = [];
var brokenURLs = [];
for (var i = 0; i < allURLs.length; i++) {
  var url = allURLs[i];
  var req = http.get(url, function (res) {
    if (res) {
      workingURLs.push(?????);  // How to derive URL from response?
    }
  });

  req.on('error', function (e) {
    brokenURLs.push(e.host);
  });
}

```

what I don't know is how to properly obtain the url from the request/ response object itself, or really how to structure this kind of async code - because again, I am a nodejs scrub :(

For most websites using res.headers.location works, but there are times when the headers do not have this property and that will cause problems for me later on. Also I've tried console logging the response object itself and that was a messy and fruitless endeavor

I have tried pushing the url variable to workingURLs, but by the time any response comes back that would trigger the push, the for loop is already over and url is forever pointing to the final element of the allURLs array.

Thanks to anyone who can help
['https://Stackoverflow.com/questions/37899964', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6062473/']
You need to closure url value to have access to it and protect it from changes on next loop iteration.  

For example: 

```
(function(url){
  // use url here
})(allUrls[i]);

```

Most simple solution for this is use `forEach` instead of `for`. 

```
allURLs.forEach(function(url){
  //....
});

```

Promisified solution allows you to get a moment when work is done:

```
    var http = require('http');
    var allURLs = [
      "http://www.yahoo.com/",
      "http://www.stackoverflow.com/",
      "http://www.sdfhksdjfksjdhg.net/"
    ];
    var workingURLs = [];
    var brokenURLs = [];
    var promises = allURLs.map(url => validateUrl(url)
      .then(res => (res?workingURLs:brokenURLs).push(url)));
    Promise.all(promises).then(() => {
      console.log(workingURLs, brokenURLs);
    });
    // ----
    function validateUrl(url) {
      return new Promise((ok, fail) => {
        http.get(url, res => return ok(res.statusCode == 200))
          .on('error', e => ok(false));
      });
    }

// Prevent nodejs from exit, don't need if any server listen.
var t = setTimeout(() => { console.log('Time is over'); }, 1000).ref();

```
You can use something like this (Not tested):

```
const arr = ["", "/a", "", ""];

Promise.all(arr.map(fetch)
.then(responses=>responses.filter(res=> res.ok).map(res=>res.url))
.then(workingUrls=>{
  console.log(workingUrls);
  console.log(arr.filter(url=> workingUrls.indexOf(url) == -1 ))
});

```

**EDITED**

[Working fiddle](https://jsfiddle.net/joherro3/x9mg92xf/2/) (Note that you can't do request to another site in the browser because of Cross domain).

**UPDATED with @vp\_arth suggestions**

```
const arr = ["/", "/a", "/", "/"];
let working=[], notWorking=[],
    find = url=> fetch(url)
    .then(res=> res.ok ? 
        working.push(res.url) && res : notWorking.push(res.url) && res);

Promise.all(arr.map(find))
.then(responses=>{
    console.log('woking', working, 'notWorking', notWorking);
    /* Do whatever with the responses if needed */
});

```

[Fiddle](https://jsfiddle.net/joherro3/x9mg92xf/3/)