How to use result outside of the query lambda?

I want get the result of query outside the lambda function. My ideal code would be like that:

let items;
wixData.query(‘RVFatia’)
.eq(‘idrvTipo’,1)
.ascending(“ordem”)
.find()
.then(res => {
items = res.items;
});
console.log(“items[0]=”+items[0]);

Declaring variable outside the lambda I can’t get the collection…
How can I workaround it?

Hi! This is probably happen due to the fact that JS is not synchronous. So, at point when you try to console.log those items, query operation is still performing, and items variable is still undefined.
That’s the reason for those "then"s actually

Read more about Promises in JS (for example) here: JavaScript Promises: an introduction

But, you can chain promises. Do smth like
.then(res => {
return res.items;
}).then(result => {
console.log(result) //will be res.items from previous “then”
}

Other way is to wait with with setTimeout - but this is very bad way of developing