0

Im connecting to a star wars api and trying to return a string with all the data, but its getting returned as undefined, but when i console.log it, it works

https = require('https')
url = 'https://swapi.co/api/people/'
getCharacters = () => {
    https.get(url, response => {
        let body = ''
        response.on('data', data => {
            json = data.toString()
            body += json
        })
        response.on('end', () => {
            data = JSON.parse(body)
            let nextLink = data.next
            return nextLink
        })
    })
}
console.log(getCharacters())

im expecting it to return a string with all the data but its getting returned as undefined

Kendall Kelly
  • 121
  • 1
  • 10
  • The HTTP call is **asynchronous**. The callbacks are invoked when the network response is received, but the call to `https.get()` returns immediately. – Pointy Sep 03 '19 at 14:34
  • @Paulpro I don't understand what u mean, i returned it with return nextLink, and when I call the function it doesn't return – Kendall Kelly Sep 03 '19 at 15:03

0 Answers0