1

I just had to do a coding test that involved a Node GET call, parsing the return data and returning it to the calling function so that it could be printed to stdout. All went fine except I couldn't figure out how to get it out of the inner asynchronous function and then get the outer function to return it. The rules of the game, as I understand them, was that I could only work on the code within my interior function.

The first iteration I came up with was something like this (I'm leaving out the tricky processing of the data which the original test required)

const https = require('https')


function inner() {
  let buff = ''


  https.get('https://jsonplaceholder.typicode.com/posts', res => {
    res.on('data', data => {
      buff += data
    })

    res.on('end', () => {
      return JSON.parse(buff)
    })


  })


}

function outer() {
  const results = inner()
}


outer()

which doesn't work, because returning from the inner function res.on('end',...) doesn't return from the outer.

Then I thought to bend the rules of the game a bit and use await, which requires me to make my inner function async. This works, up to a point. But now the async function is returning a Promise, and I have no control over the outer function to unwrap it (it's part of the test environment).

 const https = require('https')

async function inner() {
  let buff = ''

  const get = () => new Promise((resolve, reject) => {
    https.get('https://jsonplaceholder.typicode.com/posts', res => {
      res.on('data', data => {
        buff += data
      })

      res.on('end', () => {

        resolve(JSON.parse(buff))
      })


    })

  })
  try {
    const results = await get()
    return results
  } catch (e) {
    console.log(e.message)
  }


}

function outer() {
  const results = inner()
}


outer()

I'm sure there is a stupidly obvious answer, but I'm not seeing it. Perhaps there is a Promise-based method of on.('end') that eliminates these inner functions? Any help finding the best solution much appreciated (and yes, the test is over, I'm just doing this for my own knowledge).

Cerulean
  • 5,543
  • 9
  • 59
  • 111
  • @HereticMonkey -- if it's a true duplicate I'm fine with this question being closed. BUT, the tricky part about this question is that it operates within a strict environment, that is, I don't have full control over the calling function. For example, I can't make the calling function `async`,which would solve the problem. I had looked for other answers on SO prior to posting, but all of them assumed you had more control than I did. I'll look (again) at the link you posted. – Cerulean Apr 09 '19 at 18:21
  • What does 'strict environment' mean and what is this a problem? You can't make it work like `results = inner()`, no matter what you do. – Estus Flask Apr 09 '19 at 18:41
  • Strict environment means it was a coding test online, that is, I was supposed to code an inner function that made a Node GET request and return the JSON data to the testing platform. The only way I knew was to use the Node https methods above. Perhaps there's an entirely better way to solve the task – Cerulean Apr 09 '19 at 18:49
  • 1
    Asynchronous functions by definition don't complete until after the calling function returns. It's simply not possible to return the value normally. – Barmar Apr 09 '19 at 19:04
  • If the coding test is expecting that, it's either a bad test or you misunderstand. – Barmar Apr 09 '19 at 19:05

0 Answers0