API javascript calls using fetch in appstudio

I am trying to write some javascript to use in an appstudio project. I want to be able to access the data returned from the API call throughout the project - so store it in a global variable.

  1. First problem - I can’t figure out how to get the data out of the fetch call. Here is my latest attempt - it returns nothing. I also attached a project with the code that has my key code. The url works in Postman (without the cors part).
function getData() {
  return new Promise((resolve, reject) => {
    fetch("https://cors-anywhere.herokuapp.com/corsdemo/https://maps.googleapis.com/maps/api/place/textsearch/json?query=thai restaurant&key=XXXXXXXXXXX&location=41.265331,-95.949364&radius=500")
     .then(response => {
       return response.json();
      }).then(data_from_fetched => {
         let data = data_from_fetched.results;
         resolve(data);
         }
       )}
    )    
}

    // call it
    let restaurants = ""
    getData().then(data => {
        restaurants = data
    })
    console.log(restaurants)
  1. Second problem: the “https://cors-anywhere.herokuapp.com/corsdemo/” which I used to append on testing code since I’m working off my laptop doesn’t work anymore. The person who maintains it had to restrict it as it was being abused. Does anyone have another solution for testing purposes?

Thanks!
API_fetch.appstudio.zip (10.9 KB)

Tip : If you’re pasting code, html or config files, surround the code with triple back ticks (```), before the first line and after the last one. It will be formatted properly. (We fixed it for you this time)

Can you use console.log() on each of your return values to better trace what is going on?

Solution: make a new function, then call it from the last .then in getData and have ‘data’ be the paramter. In the new function put the data parameter into a global variable. Only issue - can’t access it on this form - can on another (? due to asynchronous calls?)