Skip Navigation
InitialsDiceBearhttps://github.com/dicebear/dicebearhttps://creativecommons.org/publicdomain/zero/1.0/„Initials” (https://github.com/dicebear/dicebear) by „DiceBear”, licensed under „CC0 1.0” (https://creativecommons.org/publicdomain/zero/1.0/)DA
dandimrod @lemmy.world
Posts 1
Comments 3
write to variable with fetch api before rest of script executes (client side)
  • If you want to continue in the same function you'll need to use async/await. In your case:

    async function yourFunctionName(){
        // previous logic
        const myVar = await fetch("myFile.json")
            .then(res => res.json());
        console.log(myVar);
        //rest of code executes...
    }
    

    Realize that you have to add async to your function and await to your fetch call. Also take into account that if your function returns anything, it'll now return a Promise instead.