0

There are many solutions here and elsewhere, but it seems like the simple question of actually saving a piece of fetched text or json data to a variable for later use is rarely addressed. I have this:

let ip;
const URL = 'https://api.ipify.org'
const userIp = ((url) => {
  fetch(url)
  .then(res => res.text())
  .then(data => ip = data)
  .then(() => console.log(ip)) // Logs the IP
})

// I want to use "ip" as a variable in the rest of my code
(console.log(ip) // naturally returns undefined
console.log(userIp(URL)) // Variations of this gives me the promise

I have also tried variations of the following:

const URL = 'https://api.ipify.org'
let userIp;

const getIp = (url) => {
  return fetch (url)
    .then( response => response.text())
    .then( result => result)
}

let ip = getIp(URL)
  .then(result => {
    userIp = result
  })

console.log(ip) // Promise <pending>

Have also tried async/await with the same result, the IP address logs fine inside of the function but "impossible" [EDIT: maybe my quotation marks are redundant, commenters say it's impossible to do what I wanted to do] to make use of it outside of the scope.

I have also tried to call the function where the variable will be used from inside the then(), with the variable as a parameter. But couldn't get that to work either (and besides, the variable isn't ready to be used immediately, it should be triggered by a click event).

So the question is: How should I best get an IP variable that holds my IP string to be of use by other functions further down the script?

anatolhiman
  • 1,762
  • 2
  • 14
  • 23
  • How exactly is this _not_ answered by [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call)? – Phil Apr 27 '20 at 01:38
  • See also [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Phil Apr 27 '20 at 01:40
  • @Phil I have tested many of those. If you see the code examples there is not used any variables outside of the scope of the functions that contain the data. Some of the examples simply do not address the OP's problem, as I mentioned in my post above, too. At least it is not clear for a beginner how it is done, he explains a lot of stuff that's less relevant :) – anatolhiman Apr 27 '20 at 01:44
  • 1
    What you're asking for is not possible. Asynchronous functions resolve at a later point in time, therefore you can only access resolved values _after_ that point in time. – Phil Apr 27 '20 at 01:46
  • Thanks @Phil I feared that after having read all those posts. Do you have a tip on how to get around this to be able to achieve what I'm after, to a) grab the IP string and b) use it in a function (to fetch post it to a PHP sendgrid integration). How would a pro do it if he can't save the IP for later use? – anatolhiman Apr 27 '20 at 01:50
  • Sure, `getIp(url).then(ip => useItInAFunction(ip))` – Phil Apr 27 '20 at 01:51
  • @Phil That seems to give Cannot access 'getIp' before initialization. – anatolhiman Apr 27 '20 at 02:07
  • 1
    That sounds like a different problem ([Temporal Dead Zone](https://stackoverflow.com/questions/33198849/what-is-the-temporal-dead-zone)). Whatever it is, it's not present in the code in your question – Phil Apr 27 '20 at 02:08
  • 1
    FYI, don't use global variable `URL`. It's [already a thing](https://developer.mozilla.org/en-US/docs/Web/API/URL). Also, `.then( result => result)` is totally redundant – Phil Apr 27 '20 at 02:10

0 Answers0