Why do global variables in backend modules have persistent values across devices?

I have this code in a backend/testModule.jsw file

let randomVariableA = Math.floor(Math.random() * (500 - 1 + 1) + 1);
let randomVariableB = Math.floor(Math.random() * (1000 - 1 + 1) + 1);
let randomVariableC = Math.floor(Math.random() * (1500 - 1 + 1) + 1);

export function getAllRandoms() {
    return 'Randoms are,'+randomVariableA+','+randomVariableB+','+randomVariableC;
}

When i call this using from a client side page on my website using

getAllRandoms().then((result) => {
        console.log(result);
    });

I get the exact same values across browsers and devices. I tried finding some information about this behaviour but couldn’t… Can somone explain this? And is there any other such special rules for modules I can find on some articles?

1 Like

For back end files, you should put any dynamic variable declaration inside the function block.
The global code doesn’t re-run on every call from different devices. And if you call the function from deviceA and immediately call it from deviceB it will get the same global values.
Of course, if you wait enough time between the calls, it will re-run from the the beginning but you can’t rely on that.

Thanks, that explains it, but are there any links from wix itself about any such behaviour i need to be careful about? I couldn’t find any

I don’t know. Maybe someone else will.