1

Trying to test a HTTP status code in a module but my first response is always undefined:

foo.js

const pass = require('./bar')
const test = pass('https://www.google.com/') // URL for testing will be API URL

console.log(`The return is: ${test}`)

bar.js

attempt 1:

module.exports = (url) => {
  https
    .get(url, (res) => {
      console.log(res.statusCode)
      if (typeof res.statusCode !== 'undefined' && res.statusCode.toString()[0] === '2') {
       console.log(`Results: ${res.statusCode}`)
       return true
      }
      function waiting(status) {
        if (typeof status !== 'undefined') {
          console.log('run timeout')
          setTimeout(waiting, 250)
        } else {
          console.log(`Results: ${status}`)
          return true
        }
      }
    })
    .on('error', (e) => {
      console.error(`Error ${e}`)
      return false
    })
}

attempt 2:

module.exports = (url) => {
  https
    .get(url, (res) => {
      console.log(res.statusCode)
      function waiting(status) {
        if (typeof status !== 'undefined') {
          console.log('run timeout')
          setTimeout(waiting, 250)
        } else {
          console.log(`Results: ${status}`)
          return true
        }
      }
    })
    .on('error', (e) => {
      console.error(`Error ${e}`)
      return false
    })
}

other attempts to detect undefined:

if (typeof res.statusCode === 'number' && res.statusCode !== undefined && res.statusCode !== null) {

if (!res.statusCode) {

if (typeof res.statusCode !== 'undefined') {
  console.log(`Results: ${res.statusCode}`)
  if (res.statusCode.toString()[0] === '2') return true
  return false
}

Research:

What am I doing wrong? In my module how can I check the status code after the undefined so I can return a true or false from the actual numerical value?

DᴀʀᴛʜVᴀᴅᴇʀ
  • 4,253
  • 12
  • 46
  • 84

1 Answers1

0

In both your attempts, your exported function in bar.js is not returning anything. Since you are calling an async function (https.get), you will need your exported function to be async as well. You can convert your function use promises and use async/await on the calling side. E.g.

foo.js

const pass = require('./bar');

(async function() {
    const test = await pass('https://www.google.com/'); // URL for testing will be API URL

    console.log(`The return is: ${test}`);
})();

Note the IFEE to get an async scope, as per: using await on global scope without async keyword

bar.js

const https = require('https');

module.exports = (url) => {
    return new Promise((resolve, reject) => {
        https
        .get(url, (res) => {
            console.log(res.statusCode)
            if (typeof res.statusCode !== 'undefined' && res.statusCode.toString()[0] === '2') {
                console.log(`Results: ${res.statusCode}`)
                resolve(true)
            } else {
                resolve(false)
            }
        })
        .on('error', (e) => {
            console.error(`Error ${e}`)
            resolve(false)
        })
    });
}

Alternatively, you can use a callback as follows:

foo.js

const pass = require('./bar');

pass('https://www.google.com/', test => {
    console.log(`The return is: ${test}`);
}); 

bar.js

const https = require('https');

module.exports = (url, callback) => {
    https
    .get(url, (res) => {
        console.log(res.statusCode)
        if (typeof res.statusCode !== 'undefined' && res.statusCode.toString()[0] === '2') {
            console.log(`Results: ${res.statusCode}`)
            callback(true)
        } else {
            callback(false)
        }
    })
    .on('error', (e) => {
        console.error(`Error ${e}`)
        callback(false)
    })
}
abondoa
  • 1,255
  • 9
  • 18
  • In case anyone does want to use the Async function approach be aware some [Prettier settings](https://github.com/prettier/prettier/issues/2800) will require it to be written as `;(async function() { // code})()`. – DᴀʀᴛʜVᴀᴅᴇʀ Mar 31 '20 at 22:21