0

I'm doing an API fetch and am trying to use the returned array of objects "stops" elsewhere in the global scope, however I cannot extract this array from the API fetch's scope after the .then() to use in the GLOBAL SCOPE because I am trying to use the array in other local scopes without calling the function in there.

i.e I would like to be able to use the array contained in variable stops in a separately scoped API call.

const fetchBusStopInfo = async () => {

  try {
    const url = new URL("https://data.edmonton.ca/resource/4vt2-8zrq.json")
    url.search = new URLSearchParams ({
      "$$app_token": "25u3nAznyYiBSLWIyQ4bmKnde",
      "$limit": 100000
    });

    const response = await fetch(url);
    const data = await response.json();
    return data;
  } catch (error) {
    console.log(error)
    return error;
  }
}


fetchBusStopInfo().then((stopsArray)=> {
  const stops = stopsArray
  console.log(stops)
})

Is there a way to get around this so I can use the array found in const stops elsewhere?

0 Answers0