Is there a way to store fetched API data in a global variable?

I want to fetch data from an API and store that into a variable, and export that variable to other modules for reusability.
However, I can’t seem to figure out how to reuse the data..

// getData.js
let data;
async function getData(){
  let fetchedData = await fetch('someURL').then(value => value.json());
  data = fetchedData;
}
export { data };

// otherModule.js
import { data } from './getData.js';
console.log(data);

If I do this, I know that data will not actually wait for the promise to resolve/reject and print out empty.
So, is there a way to store the fetched data in a variable that can be exported to other modules?

7 thoughts on “Is there a way to store fetched API data in a global variable?”

Leave a Comment