title: How to properly check and log http status code using promises and node.js?
body: <p>I am new to JavaScript and very new to node.js framework, just started using it a few days ago. My apologies if my code is nonsensical, the whole idea of promises and callbacks is still sinking in. That being said my question is the following I am trying to figure out if certain request to websites are successful or cause an error based on the range of their status code response. I am working with an array of websites and what I've done so far is below, I do however get a <code>TypeError: Cannot read property 'then' of undefined</code> on my local machine with node.js installed and can't figure out why.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const sample = [
      'http://www.google.com/',
      'http://www.spotify.com/us/',
      'http://twitter.com/',
      'http://google.com/nothing'
  ]
  
  const http = require('http')
  
  const getStatusCodeResult = (website) =&gt; {
  
      http.get(website, (res) =&gt; {
          return new Promise((resolve, reject) =&gt; {
              setTimeout(() =&gt; {
                  let statusCode = res.statusCode
                  error = statusCode &gt;= 400 &amp;&amp; statusCode &lt;= 500 ? `error: ${website}`: null
                  if (error) {
                      reject(error)
                      
                  } else if (statusCode &gt;= 200 &amp;&amp; statusCode &lt;= 300) {
                      resolve(`Success: ${website}`)
                  }
              }, 0)
          })
      })
  }
  // LOOP PROMISES
  const getAllStatusCodeResult = (websites) =&gt; {
      websites.forEach((website) =&gt; {
          getStatusCodeResult(website)
              .then((result) =&gt; {
                  console.log(result)
              })
              .catch(error =&gt; {
                  console.log('error', error)
              })
      })
  }
  getAllStatusCodeResult(sample)</code></pre>
  </div>
  </div>
  </p>
  
  <p>Ideally I would want the result to be printed as the example below, but for now I am just using <code>console.log</code> to figure out if the code even works.</p>
  
  <pre><code>   // Example Printout
     {
        success: ['https://www.google.com/', 'https://www.spotify.com/us/', 
        'https://twitter.com /' ],
        error: [''http://google.com/nothing']
     } 
  </code></pre>