I have pinpointed my problem to a different behaviour between IF and DO/while.
In the below code, the function chkIfExist(strRandomNum) if perfectly executed and returns a new number.
But, if I replace the IF {} with a DO {} while (remmed out), the chkIfExist(strRandomNum) function is called, but does not go into the query and does not return a result (resulting in a never ending loop).
Can anybody help me remedy this? I am clueless.
import wixData from 'wix-data';
export function button1_click(event, $w) {
wixData.query("Vouchers")
.eq("fldVoucherId", "414722")
.count()
.then((num) => {
let numberOfItems = num;
console.log("numberOfItems=" + numberOfItems);
// here is the do
// do {
if (numberOfItems > 0) {
let newNum = getRandomNum(100000, 999999).toString();
chkIfExist(newNum)
.then((amount) => {
numberOfItems = amount;
console.log("numberOfItems=" + numberOfItems);
});
}
// and while
// while (numberOfItems > 0);
});
}
function getRandomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function chkIfExist(strRandomNum) {
console.log("3: In chkIfExist");
console.log("4: strRandomNum before query= " + strRandomNum);
return wixData.query("Vouchers")
.eq("fldVoucherId", strRandomNum)
.count()
.then((amount) => {
let numCount = amount;
console.log("5: numCount=" + numCount);
let boolIdExists = (numCount > 0);
return numCount;
})
.catch((err) => {
let errorMsg = err;
console.log("errorMsg=" + errorMsg);
});
}
Hey Giri,
You know what that example did to me? 
Seriously, the issue that you’re having is that the variable numberOfItems gets changed in the .then (that is, when the promise has been fulfilled), but it is being inspected outside of the .then . I get a headache just thinking about it.
You’ll have to adjust your logic to make sure that numberOfItems is available at the correct time and isn’t waiting for its new value.
Good luck. 
Yisrael
"You know what that example did to me? "
I have got an inkling, Yisrael : I could have blow dried my hair on the combination of an overheating CPU and a ventilator spinning like mad. But maybe you refer you the rolling of your eyeballs?
To prevent my CPU burning a hole in my desk, could you indicate what you mean by the inspection and where (is it the do-loop), because, to be boringly honest, I don´t have a clue and the fun of reloading the editor every time starts to get to me after a while.
Appreciate your help, as I admitted earlier, those Promises, promises, they are not my strong point (euphemism of the day).
Hey Giri,
nice to start the day off laughing…
OK - enough frivolity…
Your problem is not trivial, but let me attempt to break it down into pieces that I can almost understand if I think hard enough.
You will first need this routine. Using async/await is a little easier in this context:
async function chkIfExist(strRandomNum) {
console.log("3: In chkIfExist");
console.log("4: strRandomNum before query= " + strRandomNum);
const amount = await wixData.query("Vouchers")
.eq("fldVoucherId", strRandomNum)
.count();
return amount;
}
Then, redo your loop to look something like this:
do {
let newNum = getRandomNum(100000, 999999).toString();
numberOfItems = await chkIfExist(newNum);
console.log("numberOfItems=" + numberOfItems);
while (numberOfItems > 0);
Disclaimer: I have not tested this code. It is only intended as a possible scenario that might work if you’re dreaming or have had enough beer. This might melt your desk or short out the power on your city block. It is not my fault. You have been warned.
Try this out, making the necessary adjustments for any errors I may have made in the above code snippets. You can read more about async/await in Wix Code to help you understand this better.
If you’re not able to work this out, please post the URL of your site so I can have the opportunity to melt my desk. Or my mind.
Yisrael
Yisrael, I had come to the same conclusion. I mistakenly assumed .then and async/await to be equivalent. I now think they can be (if there is no more code after a .then), but they don´t have to be. .then continues executing code after a .then (in my example the do-loop). Async/await really stops executing any other code UNTIL the promise is resolved. I read that article some 5 times, and it is getting clearer to me now. Thanks for your help.
If anyone runs into the same problem, the working code looks like this:
import wixData from 'wix-data';
export async function button1_click(event, $w) {
let uniqueVoucher = await getUniqueVoucherId();
console.log("uniqueVoucher main=" + uniqueVoucher);
}
async function getUniqueVoucherId() {
let numberOfItems;
let newNum;
do {
newNum = getRandomNum(100000, 999999).toString();
numberOfItems = await chkIfExist(newNum);
console.log("numberOfItems 2 (while)=" + numberOfItems);
}
while (numberOfItems > 0);
return newNum;
}
function getRandomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
async function chkIfExist(strRandomNum) {
console.log("3: In chkIfExist");
console.log("4: strRandomNum before query= " + strRandomNum);
const amount = await wixData.query("Vouchers")
.eq("fldVoucherId", strRandomNum)
.count();
console.log("amount in chkIfExist= " + amount);
return amount;
}
Giri,
Yay!
Glad you got that worked out. Ya know, we gotta have a beer together someday. Maybe several.
Yisrael 
Best idea since ages! Do prefer g&t over beer, but for you I´ll make an exception. Small detail: there´s an ocean dividing us.