0

I need to get datas sometime from a file and othertime from an api with a fetch, I tried to use a ternary operator

import datafromFile from './Data/mydata.json';
let useDatafile = false; 
const dataToUse = useDataFile ? dataStored : fetchData().catch(console.error).then(result=>result);

but is going wrong on the following lines of code cause my dataToUse is still at promise my fetchData function looks like :

const fetchData = async () => {
            const response = await fetch(targetUrl, {
                method: "GET"
            });
            const myreturn = await response.json().then(result => {
                console.log('myreturn '); console.log(result);
                return result.data;
            });

        };

I used a ternary operator cause

 var dataToUse='';
 if (useDataFile) {
     dataToUse = dataStored;
   }
    else {
          //that's is probably horrible..
          fetchData().catch(console.error).then(result => dataToUse = result);
          }

gave me a dataToUse empty .. :-(

thank you for considering my code with indulgence I am really new to javascript, thank you for reading me and for your help

0 Answers0