Home:ALL Converter>How can I fetch a javascript file and store the response object as an array?

How can I fetch a javascript file and store the response object as an array?

Ask Time:2022-01-08T02:57:08         Author:user16959178

Json Formatter

I am trying to fetch a javascript file and return the response object as an array. My javascript file is simply an array like ["1", "2", "3", ...] Here is my code right now:

function getNames() {
 let data = fetch('/path/to/file')
 .then((response) => response.json())
 .then(data => {
   console.log(data);
 return data()
 })
 .catch(error => {
   return error;
 });
}

I need to find a way to use the data variable outside of the function. How can I do this?

Author:user16959178,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/70625908/how-can-i-fetch-a-javascript-file-and-store-the-response-object-as-an-array
yy