0

[update] I am using javascript to get data and display it. i created 2 functions, DATA and DISP. i then call DATA first, then call DISP...and am having trouble getting the data from fn DATA to be available globally for later. [i will use it on a weather map]

So i have tried to update with the suggestions, and although i can now force the order using ASYNCH, I still get temp=0 when accessed later, yet it displays fine insided the DATA routine.

---why isnt the global variable TEMP being changed in setup, so i can later display it by an alert with temp= what the temp is, and not temp=0 ... note that it only works if i call the disp function inside of the setup, but i cant do this if i want to call multiple APIs to multiple cities ===== so here is the code===

var temp=0; //global
var longitude=0

function DATA()
  {
    alert(" in DATA ")
//function getWeather() {
  let temperature = document.getElementById("temperature");
  let description = document.getElementById("description");
  let location = document.getElementById("location");

  let api = "https://api.openweathermap.org/data/2.5/weather";
  let apiKey = "f146799a557e8ab658304c1b30cc3cfd";

  location.innerHTML = "Locating...";

  navigator.geolocation.getCurrentPosition(success, error);

  function success(position) {
    latitude = position.coords.latitude;
    longitude = position.coords.longitude;
  
    let url =
      api +
      "?lat=" +
      latitude +
      "&lon=" +
      longitude +
      "&appid=" +
      apiKey +
      "&units=imperial";

    fetch(url)
      .then(response => response.json())
      .then(data => {
       // console.log(data);
         temp = data.main.temp;
        temperature.innerHTML = temp + "° F";
      //DISP()  // only works here
      
        location.innerHTML =
          data.name + " (" + latitude + "°, " + longitude + "°) ";
        description.innerHTML = data.weather[0].main;
      });


  }

  function error() {
    location.innerHTML = "Unable to retrieve your location";
  }
}

//getWeather()
//}
function DISP()
{
  alert("disp temp in disp "+ temp)
tempi.innerHTML =temp;

}

async function delayedGreeting() {
DATA()
  DISP()
}

delayedGreeting();
  • 4
    Your question cannot be answered with the only the code you posted here. – Pointy Nov 25 '20 at 01:02
  • 3
    are you making an assynchronous http request ? which kind of data are you getting ? – sonEtLumiere Nov 25 '20 at 01:07
  • As per above comments, if "DATA" (`getData`) is async, then it will return without data because its async, and your "DISPLAY" (`displayData`) will get called immediately, even if there is no data yet. ... So you get 0, on what presumable is already scoped for `data`. ..... But since this "pseudocode" really, an adequate solution would help if you provided your actual source. – GetSet Nov 25 '20 at 01:25
  • For handling asynchronous functions, have a look here: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323 – ΔO 'delta zero' Nov 25 '20 at 01:33

1 Answers1

0

try to fix :

function displaydata(data){
...}

Also if you are working with async functions ( maybe when you try to get data from the web) make sure you use await . await holds the process until it get's the promise back. Also it might be helpful the add some more of the code .

  • so i added the await , as follows, and my value of the global variable is still zero 0. async function delayedGreeting() { await DATA() await DISP() alert( " asynch done") } – Rich Engle Nov 26 '20 at 01:29