ajax - Handling Json data outside a function with pebble.js -


in function getweather() fetch weather data json-object , store data in variable data. within function, can handle json-data, how access data outside of getweather()?

it doesn't matter if return variable location or data. variable place not json.

how handle place variable in order make work within function getweather()?

var ajax = require('ajax');  var place; place = getweather();  function getweather() {     // make request     ajax(     {       url: 'http://api.openweathermap.org/data/2.5/weather?q=paris.fr', type: 'json'    },     function(data)      {     // success!     console.log("successfully fetched weather data!");      // extract data         var location = data.name;         return location;          },     function(error) {     // failure!     console.log('failed fetching weather data: ' + error);     }   );     } 

the in ajax stands asynchronous. what's happening in code trying assign value place before asynchronous call api.openweather.org has returned.

you can check placing statement console.log(place); directly after place = getweather();. notice in console none returned before see successfully fetched weather data!

this issue has been described in detail in this post. recommends restructuring code around callbacks.


Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -