I want to get the browser’s height and width. I used wix-window module and followed the documentation, but it looks like I cannot access the windowHeight and windowWidth variables outside the then() function. I want to assign the window’s height and width to the global x and y variables so I can use it later in my code.
Here is my code:
var x,y;
wixWindow.getBoundingRect()
.then( (windowSizeInfo) => {
let windowHeight = windowSizeInfo.window.height;
let windowWidth = windowSizeInfo.window.width;
x = windowWidth;
y = windowHeight
});
console.log(x, y)
In console, this code just prints undefined, undefined
Here a simple example, similar to your needs…
$w.onReady(async function () {
console.log("DisplayResolution: ", await getScreenSize())
});
function getScreenSize() {
let windowHeight
let windowWidth
let DisplayResolution
return wixWindow.getBoundingRect()
.then((windowSizeInfo) => {
windowHeight = windowSizeInfo.window.height;
windowWidth = windowSizeInfo.window.width;
DisplayResolution = windowWidth+"x"+windowHeight
// let x = windowWidth;
// let y = windowHeight
return (DisplayResolution)
});
}
Hey Narendra 
Your code is okay, but … The problem is that you’re not waiting for the promise to be resolved which means that (" x " and " y ") are printed to the console before the promise is resolved, to get the results after the the values are fetched, you need to wait for them with the help of async/await , that way, the code will wait until the function is resolved, then it’ll continue to the next line of code, which is printing the values.
Try converting the parent function to an async function and wait for the promise to be resolved.
$w.onReady(async () => {
let x, y;
await wixWindow.getBoundingRect().then((result) => {
const window = result.window;
x = window.width;
y = window.height;
})
})
Hope this helps~!
Ahmad