0

this is my problem: I have a json file and i want import into my JS file

{
    "1": {
        "Nome": "Umani",
        "Stat": {
            "Volonta": 1
        },
        "AbiInn": {
            "Resistenza": 1,
            "Concentrazione": 1
        },
        "AbiNot": [],
        "Adv": [
            "Nessuno"
        ],
        "Dis": [
            "Nessuno"
        ]
    },
    "2": {
        "Nome": "Elfi",
        "Stat": {
            "Riflessi": 1
        },
        "AbiInn": {
            "Destrezza": 1,
            "Incantesimi": 1
        },
        "AbiNot": [],
        "Adv": [
            "Visione Crepuscolare"
        ],
        "Dis": [
            "Nessuno"
        ]
    }

since i have many calls to in my JS i wanted it to be writen to a constant to easy access it and dont have it into the JS file that steal a lof of space.

    function fLoadFile(fFileUrl,fKeyDest) {
        let tReturn = '';
        fetch(fFileUrl)
        .then(response => response.json())
        .then(data => {
            console.log('Dati Ricevuti: ', data);
            tReturn = data;
        });
        return tReturn;
    }
    const scheda = fLoadFile("./json/d10s-scheda.json");
    const razze = fLoadFile("./json/d10s-razze.json");
    const archetype = fLoadFile("./json/d10s-arch.json");

if i use console.log(tReturn); inside the function it shows me the correct content it should have. when i use console.log(scheda); it shows me a promise.

i looked deep into Stackoverflow looking for alternatives

i tryed require(); and does not work

i've been trying to fix this for past 2 days and i am out of options.

i really hope you guys can help me with this, i am new to JS. Also i apologize in advance if my english is not good, it is not my main language, i am italian. Many Thanks

  • "when i use console.log(scheda); it shows me a promise." — It should show you an empty string (make sure that the code you are showing us is the code that produced the results you are telling us about and not a different iteration of that code). – Quentin Apr 08 '23 at 15:11

0 Answers0