Check current positions if any

Hello, I’m trying to understand how to check current position and use that variable outside of the function. I use this code:

function rebalance() {
try {
alpaca.getPosition(stock).then((resp) => {
position = resp.qty;
console.log(“Position quantity from rebalance function”, position);

});

} catch (err) {
// console.log(err);
errors = err.error[code];
if (errors == 40410000) {
position = 0;
console.log("Position: ", position);
}
}
}
//…

rebalance();

if (position == 0 ) { //Execute trade}
if( position >0) || (position <0) { // Trade is already executed}

How to get position variable outside of that function?

Hi,
you could just do the last part inside rebalance too (also it makes sense to change position inside a rebalance function)