简体   繁体   中英

How to store data from a async function in a new array?

I'm pretty new to this and I'm stuck. I try to understand how I can store data from a json file in a new variable. I can log this out to the console, but I want to get this array in a new variable. Every video or tutorial I watched just did that... logs it out to the console.

Here is my code

const getData = async () => {

    const response = await fetch ('data.json');
    const data = await response.json();
    return data;
    
};

getData()
    .then((data)=> console.log(data));

Thanks for your help!

Here you go, but as @Guerric P mentioned in the comments, you probably must ask yourself why you need to do this

 const getData = async () => { const response = await fetch ('data.json'); const data = await response.json(); return data; }; const putDataToVariable = async () => { const variable = await getData() console.log(variable) } putDataToVariable().then()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM